Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I would say it is familiarity, but in another sense. You can use something like

    path.file_name().and_then(|f| f.to_str()).unwrap()
but that really doesn't buy you much.

Where familiarity comes in I believe is in what that line communicates to me: There might not be a file name (like having '/' as path), and that getting a string slice out of it might fail (the kind of string the platform uses for paths might not be UTF-8 compatible).

Plus, the unwrap()s tell me that whoever wrote the code considered it an assertion failure if one of those is happening.

I read a lot more Rust than I write, and it really helps that the code communicates a lot. And with knowing about OsString/OsStr beforehand, I only had to lookup when file_name() returns None, the rest is rather implicit by convention, or familiarity.



I wish `and_then` were named `flat_map` so badly.


We have `flat_map` for iterators. (`and_then` works for Options, which are basically nullables)

You can convert an Option to a zero-cost iterator and then use `flat_map` though.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: