January 2019
Beginner to intermediate
554 pages
13h 31m
English
Let's look at some of the useful combinators that are available for both the Option and Result types:
map: This method allows you to transform the success value, T, to another value, U. The following is the type signature of map for the Option type:
pub fn map<U, F>(self, f: F) -> Option<U>where F: FnOnce(T) -> U { match self { Some(x) => Some(f(x)), None => None, }}
The following is the signature for the Result type:
pub fn map<U, F>(self, f: F) -> Option<U>where F: FnOnce(T) -> U { match self { Ok(t) => Ok(f(t)), Err(e) => Err(e) }}
This method's type signature can be read as follows: map is a generic method over U and F, and takes self by value. It then takes a parameter, f, of type F and returns an Option<U>, where ...
Read now
Unlock full access