pub trait VecExt {
type Item;
fn into_map<B, F>(self, f: F) -> Vec<B>
where
F: FnMut(Self::Item) -> B;
fn into_try_map<B, E, F>(self, f: F) -> Result<Vec<B>, E>
where
F: FnMut(Self::Item) -> Result<B, E>;
}
Expand description
Extension traits on Vec
.
Required Associated Types
Required Methods
sourcefn into_map<B, F>(self, f: F) -> Vec<B>where
F: FnMut(Self::Item) -> B,
fn into_map<B, F>(self, f: F) -> Vec<B>where
F: FnMut(Self::Item) -> B,
A shorthand for into_iter().map(f).collect::<Vec<_>>()
. For example:
use gazebo::prelude::*;
assert_eq!(vec![1,2,3].into_map(|x| x*x), vec![1,4,9]);
sourcefn into_try_map<B, E, F>(self, f: F) -> Result<Vec<B>, E>where
F: FnMut(Self::Item) -> Result<B, E>,
fn into_try_map<B, E, F>(self, f: F) -> Result<Vec<B>, E>where
F: FnMut(Self::Item) -> Result<B, E>,
A shorthand for into_iter().map(f).collect::<Result<Vec<_>, _>>()
. For example:
use gazebo::prelude::*;
assert_eq!(vec![1,2,3].into_try_map(|x| Ok(x*x)), Ok::<_, bool>(vec![1,4,9]));
assert_eq!(vec![1,2,-3].into_try_map(|x| if x > 0 { Ok(x*x) } else { Err(false) }), Err(false));
This function will be generalised to Try
once it has been
standardised.