Trait OptionRefMap

Source
pub trait OptionRefMap<'o, T: 'o> {
    // Required method
    fn ref_map<U, F>(&'o self, f: F) -> Option<U>
       where F: FnOnce(&'o T) -> U;
}
Expand description

Extension trait for Option. Adds the ref_map() extension method onto Option.

The ref_map() method borrows the internal object and passes it to a closure to be mapped in some way. This allows convenient use of as_* type methods or calculations which require the borrowed internal value.

use ref_map::*;

let values = Some(vec![4, 7, 9, 5, 6]);

let filtered = values.ref_map(|v| &v[3..]);
let answer = &[5, 6];

assert_eq!(filtered, Some(&answer[..]));

See the crate-level documentation for more information.

Required Methods§

Source

fn ref_map<U, F>(&'o self, f: F) -> Option<U>
where F: FnOnce(&'o T) -> U,

Borrows the internal value and maps it using the provided closure.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'o, T: 'o> OptionRefMap<'o, T> for Option<T>

Source§

fn ref_map<U, F>(&'o self, f: F) -> Option<U>
where F: FnOnce(&'o T) -> U,

Implementors§