Using combinators

Using the map method is simple:

// using_map.rs

fn get_nth(items: &Vec<usize>, nth: usize) -> Option<usize> {
if nth < items.len() {
Some(items[nth])
} else {
None
}
}

fn double(val: usize) -> usize {
val * val
}

fn main() {
let items = vec![7, 6, 4, 3, 5, 3, 10, 3, 2, 4];
println!("{}", items.len());
let doubled = get_nth(&items, 4).map(double);
println!("{:?}", doubled);
}

In the preceding code, we have a method called get_nth that gives us the nth element from Vec<usize> and returns None if it couldn't find one. We then have a use case where we want to double the value. We can use the map method on the return value of get_nth, passing in the double function we defined previously. Alternatively, we could have provided a closure written inline, like the following:

let doubled = get_nth(&items, 10).map(|v| v * v);

This is quite a concise way to chain operations! This is less verbose than using match or if let.

The preceding explanation of the map method is very much applicable to the next set of methods that we'll look at, so we'll skip explaining their type signature as it would be too noisy for us to go through every one of them. Instead, we'll just explain briefly the functionality that's provided by these methods. You are encouraged to read and become familiar with their type signature by referring to their documentation:

  • map_err: This method acts only on Result types and allows transforming the failed value from E to some other type, H, but only if the value is an Err value. map_err is not defined for Option types, as doing anything with None would be pointless.
  • and_then: In the case of a failed value, this returns the value as is, but in the case of a successful value, this takes in a closure as the second argument, which acts on the wrapped value and returns the wrapped type. This is useful when you need to perform transformations on the inner values, one after another.
  • unwrap_or: This method extracts the inner success value, or returns a default one if it's a failed value. You provide the default value to it as a second argument.
  • unwrap_or_else: This method acts the same as the preceding method but computes a different value when it is a failed value by taking a closure as the second argument.
  • as_ref: This method converts the inner value to a reference and returns the wrapped value, that is, an Option<&T> or a Result<&T, &E>.
  • or/ or_else: These methods return the value as is if it's a success value, or returns an alternative Ok/Some value, which is provided as the second argument. or_else accepts a closure within which you need to return a success value.
  • as_mut: This method converts the inner value into a mutable reference and returns the wrapped value, that is, an Option<&mut T> or a Result<&mut T, &mut E>.

There are many more that are unique to the Option and Result types. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset