Trait gazebo::prelude::SliceExt [−][src]
pub trait SliceExt { type Item; fn map<'a, B, F>(&'a self, f: F) -> Vec<B>
where
F: FnMut(&'a Self::Item) -> B; fn try_map<'a, B, E, F>(&'a self, f: F) -> Result<Vec<B>, E>
where
F: FnMut(&'a Self::Item) -> Result<B, E>; fn as_singleton(&self) -> Option<&Self::Item>; fn cloned(&self) -> Vec<Self::Item>
where
Self::Item: Clone, { ... } fn duped(&self) -> Vec<Self::Item>
where
Self::Item: Dupe, { ... } fn owned<'a, T, R>(&'a self) -> Vec<R>
where
Self::Item: TEq<&'a T>,
R: Borrow<T>,
T: ToOwned<Owned = R>,
T: 'a,
T: ?Sized, { ... } }
Expand description
Extension traits on slices/Vec
.
Associated Types
Required methods
A shorthand for iter().map(f).collect::<Vec<_>>()
. For example:
use gazebo::prelude::*; assert_eq!([1,2,3][..].map(|x| x*x), vec![1,4,9]); assert_eq!(vec![1,2,3].map(|x| x*x), vec![1,4,9]);
Note that from Rust 1.47.0 there is a map
method on
arrays (e.g. [T; N]
) behind the array_map
feature flag.
Either enable this function (which would be into_map
in our
vocabulary), or explicitly convert arrays to slices with the
[..]
operation.
A shorthand for iter().map(f).collect::<Result<Vec<_>, _>>()
. For example:
use gazebo::prelude::*; assert_eq!([1,2,3].try_map(|x| Ok(x*x)), Ok::<_, bool>(vec![1,4,9])); assert_eq!([1,2,-3].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.
fn as_singleton(&self) -> Option<&Self::Item>
fn as_singleton(&self) -> Option<&Self::Item>
If the size of vector is 1, returns the first element Otherwise, returns None
use gazebo::prelude::*; assert_eq!(*vec![1].as_singleton().unwrap(), 1); assert_eq!(vec!['a', 'b', 'c'].as_singleton(), None);
Provided methods
Clone each element within a vector using clone
. For example:
use gazebo::prelude::*; let xs: Vec<String> = vec![String::from("hello"), String::from("world")]; let ys: Vec<String> = xs.cloned(); assert_eq!(xs, ys);
Duplicate each element within a vector using dupe
. For example:
use gazebo::prelude::*; use std::sync::Arc; let xs: Vec<Arc<String>> = vec![Arc::new(String::from("hello"))]; let ys: Vec<Arc<String>> = xs.duped(); assert_eq!(xs, ys);