[−][src]Trait core_extensions::option_result_ext::OptionExt
Extension trait for Option.
Provided methods
pub fn filter_<F>(self, predicate: F) -> Option<T> where
F: FnOnce(&T) -> bool,
[src]
F: FnOnce(&T) -> bool,
Allows using Option::filter before Rust 1.27.
Example
use core_extensions::OptionExt; let text="what the "; assert_eq!(Some(text).filter_(|x| x.len()==9 ).is_some(),true); assert_eq!( text.split_whitespace().next() .filter_(|x| x.len()==4 ), Some("what")); assert_eq!(Some(text).filter_(|x| x.len()==20 ),None); assert_eq!( text.split_whitespace().next() .filter_(|x| x.len()==10 ), None);
pub fn map_ref<'a, U, F>(&'a self, f: F) -> Option<U> where
T: 'a,
F: FnOnce(&'a T) -> U,
[src]
T: 'a,
F: FnOnce(&'a T) -> U,
Maps as reference to the contents.
Example
use core_extensions::OptionExt; struct User{ name:String, surname:String, } let user=Some(User{name:"Matt".into(),surname:"Parker".into()}); let name =user.map_ref(|v| v.name.as_str() ); let surname=user.map_ref(|v| v.surname.as_str() ); assert_eq!(name,Some("Matt")); assert_eq!(surname,Some("Parker"));
pub fn map_mut<'a, U, F>(&'a mut self, f: F) -> Option<U> where
T: 'a,
F: FnOnce(&'a mut T) -> U,
[src]
T: 'a,
F: FnOnce(&'a mut T) -> U,
Maps as mutable reference to the contents.
Example
use core_extensions::OptionExt; struct User{ name:String, surname:String, } let mut user=Some(User{name:"Matt".into(),surname:"Parker".into()}); { let name =user.map_mut(|v|{ v.name.push_str("hew") ; v.name.as_str() }); assert_eq!(name,Some("Matthew")); } assert_eq!(user.unwrap().name,"Matthew");