3 ms·
I'm not very familiar with Rust. Do the built in Option and Result types not implement map and flatMap?
by tengbretson 1y ago
I'm not very familiar with Rust. Do the built in Option and Result types not implement map and flatMap?
- trealira 1y agoThey do implement both of those, except instead of flat_map, it's called and_then for Option and Result.
- theon144 1y agoThey 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.
- Sharlin 1y agoAs 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.