Combinators on Option/Result
As Option
and Result
are wrapper types, the only way to safely interact with their inner values is either through pattern matching or if let
. This paradigm of using matching and then acting on the inner values is a very common operation and, as such, it becomes very tedious having to write them every time. Fortunately, these wrapper types come with lots of helper methods, also known as combinators, implemented on them that allow you to manipulate the inner values easily.
These are generic methods and there are many kinds depending on the use case. Some methods act on success values, such as Ok(T)
/Some(T)
, while some of them act on failed values, such as Err(E)
/None
. Some methods unwrap and extract the inner value, while some preserve the structure of the wrapper type modifying just the inner values.
Note
Note: In this section, when we talk about success values, we are commonly referring to Ok(T)
/Some(T)
variants and when we talk about failed values, we are referring...