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

I'm not very familiar with Rust. Do the built in Option and Result types not implement map and flatMap?


They do, `map` and `and_then`.

As for the article, I'm also a bit confused because I'm really not sure whether people write that sort of code at the beginning "very commonly" - match and `ok_or` to handle None by turning them into proper Errors is one of the first things you learn in Rust.


As others have said, you can `and_then` chain `Options`, but often it’s better to convert each `Option` into a `Result`s before chaining, to get more fine-grained error messages as shown in the fine article. But usually it’s cleaner and more convenient (and friendlier to people used to exceptions) to use the `?` operator which is basically Rust’s `do` notation except that currently you can only early-return from the entire function with it, not escape a specific block. Which in turn requires the types to match, though Rust does at least insert an `.into()` conversion for the error value.


They do implement both of those, except instead of flat_map, it's called and_then for Option and Result.




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

Search: