pub struct Vec<N, A>where
N: Unsigned,{ /* private fields */ }
Expand description
A vector of length N
containing elements of type A
.
Implementations§
Source§impl<N, A> Vec<N, A>where
N: Unsigned,
impl<N, A> Vec<N, A>where
N: Unsigned,
Sourcepub fn fill<F>(f: F) -> Vec<N, A>
pub fn fill<F>(f: F) -> Vec<N, A>
Construct a vector of size N
using a function.
The function is called with an index to generate a value of A
for each
index in the vector.
§Examples
let vec: Vec<U3, _> = Vec::fill(|i| i + 10);
assert_eq!(svec![10, 11, 12], vec);
Sourcepub fn repeat(a: A) -> Vec<N, A>where
A: Clone,
pub fn repeat(a: A) -> Vec<N, A>where
A: Clone,
Construct a vector of size N
containing the same element repeated.
§Examples
let vec: Vec<U3, _> = Vec::repeat(5);
assert_eq!(svec![5, 5, 5], vec);
Sourcepub fn try_from_iter<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = A>,
pub fn try_from_iter<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = A>,
Construct a vector of size N
from an iterator.
Returns None
if the iterator didn’t contain exactly N
elements.
§Examples
let good_vec: Option<Vec<U3, _>> = Vec::try_from_iter(1..=3);
assert_eq!(Some(svec![1, 2, 3]), good_vec);
let bad_vec: Option<Vec<U3, _>> = Vec::try_from_iter(1..=500);
assert_eq!(None, bad_vec);
Sourcepub fn from_default() -> Vec<N, A>where
A: Default,
pub fn from_default() -> Vec<N, A>where
A: Default,
Construct a vector of size N
using the default value.
§Examples
let vec: Vec<U3, _> = Vec::from_default();
assert_eq!(svec![0, 0, 0], vec);
Sourcepub fn pop(self) -> (Vec<Diff<N, U1>, A>, A)
pub fn pop(self) -> (Vec<Diff<N, U1>, A>, A)
Pop an element off the end of the vector.
§Examples
let vec = svec![1, 2, 3];
let (new_vec, value) = vec.pop();
assert_eq!(svec![1, 2], new_vec);
assert_eq!(3, value);
let vec: Vec<U3, _> = svec![1, 2, 3];
// Type error, because the new length will be U2, not U3:
let (new_vec: Vec<U3, _>, value) = vec.pop();
Sourcepub fn insert<Index>(self, _: Index, a: A) -> Vec<Add1<N>, A>
pub fn insert<Index>(self, _: Index, a: A) -> Vec<Add1<N>, A>
Insert an element into the vector at index Index
.
Sourcepub fn remove<Index>(self, _: Index) -> (Vec<Sub1<N>, A>, A)
pub fn remove<Index>(self, _: Index) -> (Vec<Sub1<N>, A>, A)
Remove the element at index Index
from the vector.
Sourcepub fn swap_remove<Index>(self, _: Index) -> (Vec<Sub1<N>, A>, A)
pub fn swap_remove<Index>(self, _: Index) -> (Vec<Sub1<N>, A>, A)
Remove the element at index Index
from the vector,
replacing it with the element at the end of the vector.
Sourcepub fn get_mut<Index>(&mut self, _: Index) -> &mut A
pub fn get_mut<Index>(&mut self, _: Index) -> &mut A
Get a mutable reference to the element at index Index
.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, A>
pub fn iter_mut(&mut self) -> IterMut<'_, A>
Get a mutable iterator over the elements of the vector.
Sourcepub fn as_slice(&self) -> &[A]
pub fn as_slice(&self) -> &[A]
Get a reference to the slice of elements contained in the vector.
Sourcepub fn as_mut_slice(&mut self) -> &mut [A]
pub fn as_mut_slice(&mut self) -> &mut [A]
Get a mutable reference to the slice of elements contained in the vector.
Sourcepub fn truncate<M>(self) -> Vec<M, A>
pub fn truncate<M>(self) -> Vec<M, A>
Truncate the vector to fit the size given by the target type.
§Examples
let vec_6 = svec![1, 2, 3, 4, 5, 6];
let vec_3: Vec<U3, _> = vec_6.truncate();
assert_eq!(svec![1, 2, 3], vec_3);
Sourcepub fn slice<Left, Right>(
self,
_: Range<Left, Right>,
) -> (Vec<Diff<N, Diff<Right, Left>>, A>, Vec<Diff<Right, Left>, A>)
pub fn slice<Left, Right>( self, _: Range<Left, Right>, ) -> (Vec<Diff<N, Diff<Right, Left>>, A>, Vec<Diff<Right, Left>, A>)
Slice a subset out of the vector.
§Examples
let vec = svec![1, 2, 3, 4, 5, 6];
let (vec, sub_vec) = vec.slice(Range::<U2, U4>::new());
assert_eq!(svec![1, 2, 5, 6], vec);
assert_eq!(svec![3, 4], sub_vec);
Sourcepub fn drain<Left, Right>(
self,
range: Range<Left, Right>,
) -> (Vec<Diff<N, Diff<Right, Left>>, A>, impl Iterator<Item = A>)
pub fn drain<Left, Right>( self, range: Range<Left, Right>, ) -> (Vec<Diff<N, Diff<Right, Left>>, A>, impl Iterator<Item = A>)
Remove a subset from the vector and return an iterator over the removed elements.
§Examples
let vec = svec![1, 2, 3, 4, 5, 6];
let (vec, iter) = vec.drain(Range::<U2, U4>::new());
assert_eq!(svec![1, 2, 5, 6], vec);
assert_eq!(vec![3, 4], iter.collect::<::std::vec::Vec<_>>());
Sourcepub fn split_off<Index>(
self,
_: Index,
) -> (Vec<Index, A>, Vec<Diff<N, Index>, A>)
pub fn split_off<Index>( self, _: Index, ) -> (Vec<Index, A>, Vec<Diff<N, Index>, A>)
Split the vector at Index
, returning the two sides of the split.
§Examples
let vec = svec![1, 2, 3, 4, 5, 6];
let (left, right) = vec.split_off(U3::new());
assert_eq!(svec![1, 2, 3], left);
assert_eq!(svec![4, 5, 6], right);
Sourcepub fn resize<M>(self, value: A) -> Vec<M, A>
pub fn resize<M>(self, value: A) -> Vec<M, A>
Resize the vector, dropping elements if the new size is smaller,
and padding with copies of value
if it is larger.
§Examples
let vec = svec![1, 2, 3];
let vec: Vec<U5, _> = vec.resize(100);
assert_eq!(svec![1, 2, 3, 100, 100], vec);
Sourcepub fn map<F, B>(self, f: F) -> Vec<N, B>where
F: FnMut(A) -> B,
pub fn map<F, B>(self, f: F) -> Vec<N, B>where
F: FnMut(A) -> B,
Map the vector into a vector of elements of B
using a function.
§Examples
let vec = svec![1, 2, 3];
let vec = vec.map(|num| format!("{}", num));
assert_eq!(svec![
"1".to_string(), "2".to_string(), "3".to_string()
], vec);
Sourcepub fn apply<F, B>(self, fs: Vec<N, F>) -> Vec<N, B>where
F: FnMut(A) -> B,
pub fn apply<F, B>(self, fs: Vec<N, F>) -> Vec<N, B>where
F: FnMut(A) -> B,
Apply a list of functions from A
to B
to a vector of A
in order,
returning a vector of B
.
§Examples
let vec = svec![1, 2, 3];
let fn_vec = vec.clone().map(|i| move |a| a + i);
let vec = vec.apply(fn_vec);
assert_eq!(svec![2, 4, 6], vec);
Sourcepub fn zip<B, C, F>(self, other: Vec<N, B>, f: F) -> Vec<N, C>where
F: FnMut(A, B) -> C,
pub fn zip<B, C, F>(self, other: Vec<N, B>, f: F) -> Vec<N, C>where
F: FnMut(A, B) -> C,
Merge two vectors together into a new vector using a function.
§Examples
let left = svec!["foo", "bar"];
let right = svec!["lol", "omg"];
let vec = left.zip(right, |a, b| format!("{} {}", a, b));
assert_eq!(svec![
"foo lol".to_string(), "bar omg".to_string()
], vec);
Trait Implementations§
Source§impl<N, A> BorrowMut<[A]> for Vec<N, A>where
N: Unsigned,
impl<N, A> BorrowMut<[A]> for Vec<N, A>where
N: Unsigned,
Source§fn borrow_mut(&mut self) -> &mut [A]
fn borrow_mut(&mut self) -> &mut [A]
Source§impl<'a, N, A> IntoIterator for &'a Vec<N, A>where
N: Unsigned,
impl<'a, N, A> IntoIterator for &'a Vec<N, A>where
N: Unsigned,
Source§impl<'a, N, A> IntoIterator for &'a mut Vec<N, A>where
N: Unsigned,
impl<'a, N, A> IntoIterator for &'a mut Vec<N, A>where
N: Unsigned,
Source§impl<N, A> IntoIterator for Vec<N, A>where
N: Unsigned,
impl<N, A> IntoIterator for Vec<N, A>where
N: Unsigned,
Source§impl<N, A: Ord> Ord for Vec<N, A>
impl<N, A: Ord> Ord for Vec<N, A>
Source§impl<N, A: PartialOrd> PartialOrd for Vec<N, A>where
N: Unsigned + PartialOrd,
impl<N, A: PartialOrd> PartialOrd for Vec<N, A>where
N: Unsigned + PartialOrd,
Source§impl<'a, N, A> TryFrom<&'a [A]> for Vec<N, A>
impl<'a, N, A> TryFrom<&'a [A]> for Vec<N, A>
Source§fn try_from(slice: &'a [A]) -> Result<Self, Self::Error>
fn try_from(slice: &'a [A]) -> Result<Self, Self::Error>
Construct a vector of size N
from a slice of clonable values.
Returns Err(())
if the source slice didn’t contain exactly N
elements.
§Examples
let good_vec: Result<Vec<U3, _>, _> = Vec::try_from([1, 2, 3].as_ref());
assert_eq!(Ok(svec![1, 2, 3]), good_vec);
let bad_vec: Result<Vec<U3, _>, _> = Vec::try_from([1, 2].as_ref());
assert_eq!(Err(()), bad_vec);
Source§impl<N, A> TryFrom<Box<[A]>> for Vec<N, A>where
N: Unsigned,
impl<N, A> TryFrom<Box<[A]>> for Vec<N, A>where
N: Unsigned,
Source§fn try_from(array: Box<[A]>) -> Result<Self, Self::Error>
fn try_from(array: Box<[A]>) -> Result<Self, Self::Error>
Construct a vector of size N
from a boxed array.
Returns Err(())
if the source vector didn’t contain exactly N
elements.
§Examples
let boxed: Box<[_]> = Box::new([1, 2, 3]);
let good_vec: Result<Vec<U3, _>, _> = Vec::try_from(boxed);
assert_eq!(Ok(svec![1, 2, 3]), good_vec);
let boxed: Box<[_]> = Box::new([1, 2]);
let bad_vec: Result<Vec<U3, _>, _> = Vec::try_from(boxed);
assert_eq!(Err(()), bad_vec);
Source§impl<N, A> TryFrom<Vec<A>> for Vec<N, A>where
N: Unsigned,
impl<N, A> TryFrom<Vec<A>> for Vec<N, A>where
N: Unsigned,
Source§fn try_from(vec: Vec<A>) -> Result<Self, Self::Error>
fn try_from(vec: Vec<A>) -> Result<Self, Self::Error>
Construct a vector of size N
from a std::vec::Vec
.
Returns Err(())
if the source vector didn’t contain exactly N
elements.
§Examples
let good_vec: Result<Vec<U3, _>, _> = Vec::try_from(vec![1, 2, 3]);
assert_eq!(Ok(svec![1, 2, 3]), good_vec);
let bad_vec: Result<Vec<U3, _>, _> = Vec::try_from(vec![1, 2]);
assert_eq!(Err(()), bad_vec);