Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: When is mapping better for error handling than immediate ```match``` or propagation?
Hello everyone!
In Rust, the core::result::Result
enum is used for reporting and handling errors. Typically, when the fallible function returns an error, its caller propagates it further with ?
operator (if it doesn't have enough context to handle the error) or uses match
of if let
to handle the error (similar to exceptions in other languages). However, Rust also has the alternative -- map()
, and_then()
and or_then()
methods on Result
objects (and similar), in which the fallible process can be split into parts, and the next part is called only if the previous one had succeeded. It can be used in command-line utilities or fallible network or IPC request handlers, when successes and failures are reported to some external destination, and the code looks more clean to me with mapping than with matching (and returning on failure) on each fallible function call. I wonder, in which cases is it better to handle errors with immediate matching, and in which cases mapping is better?
P.S. I also wonder, if a function accepts a closure through a generic type implementing FnOnce
trait, can such closure be inlined?
7 posts - 5 participants
🏷️ Rust_feed