Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve unix/ls task #698

Merged
merged 5 commits into from Jan 20, 2020
Merged

improve unix/ls task #698

merged 5 commits into from Jan 20, 2020

Conversation

ghost
Copy link

@ghost ghost commented Jan 18, 2020

  • 2018 edition
  • slightly more functional style
  • using ? operator over unwrap()

Rust 2018 lets you use this when defining the main return signature
accordingly.
fn print_files(path: &Path) -> std::io::Result<()> {
// flatten entries to avoid calling .unwrap()
let mut entries: Vec<_> = fs::read_dir(path)?
.flat_map(|res| res)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: I still am learning flat_map and iterator chains. Something feels odd about needing to call both of them. I had tried map_or but couldn't call that directly on the ReadDir iterator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can combine them:

        .flat_map(|entry| {
            entry.ok().map(|entry| entry.file_name())
        })

@euclio
Copy link
Member

euclio commented Jan 20, 2020

bors r+

bors bot added a commit that referenced this pull request Jan 20, 2020
698: improve unix/ls task r=euclio a=efx

- 2018 edition
- slightly more functional style
- using `?` operator over `unwrap()`

Co-authored-by: Eli Flanagan <efx@users.noreply.github.com>
@bors
Copy link
Contributor

bors bot commented Jan 20, 2020

@bors bors bot merged commit 15c77f5 into rust-rosetta:master Jan 20, 2020
@ghost ghost deleted the unix-ls-2018 branch January 20, 2020 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants