pub struct NonEmptyVec<T> { /* private fields */ }Expand description
A vector type which is guaranteed to be non-empty.
New instances must be created with an initial element to ensure that the
vector is non-empty. This means that the methods first and last always
produce an element of type T.
let v = NonEmptyVec::new(1);
assert_eq!(*v.first(), 1);
assert_eq!(*v.last(), 1);It is possible to push new elements into the vector, but pop will return
None if there is only one element left to ensure that the vector is always
nonempty.
let mut v = NonEmptyVec::new(1);
v.push(2);
assert_eq!(v.pop(), Some(2));
assert_eq!(v.pop(), None);Implementations§
source§impl<T> NonEmptyVec<T>
impl<T> NonEmptyVec<T>
pub fn new(x: T) -> NonEmptyVec<T>
pub fn first(&self) -> &T
pub fn first_mut(&mut self) -> &mut T
sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Pops the last element of the vector.
This method will return None when there is one element left in the
vector to ensure that the vector remains non-empty.
let mut v = NonEmptyVec::new(1);
v.push(2);
assert_eq!(v.pop(), Some(2));
assert_eq!(v.pop(), None);sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the vector.
let mut v = NonEmptyVec::new(1);
v.push(2);
v.push(3);
assert_eq!(v.len(), 3);sourcepub fn iter(&self) -> NonEmptyIter<'_, T> ⓘ
pub fn iter(&self) -> NonEmptyIter<'_, T> ⓘ
Returns an iterator over the vector.
Trait Implementations§
source§impl<T: Clone> Clone for NonEmptyVec<T>
impl<T: Clone> Clone for NonEmptyVec<T>
source§fn clone(&self) -> NonEmptyVec<T>
fn clone(&self) -> NonEmptyVec<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl<T> From<NonEmptyVec<T>> for Vec<T>
impl<T> From<NonEmptyVec<T>> for Vec<T>
source§impl<T> Index<&usize> for NonEmptyVec<T>
impl<T> Index<&usize> for NonEmptyVec<T>
source§impl<T> Index<usize> for NonEmptyVec<T>
impl<T> Index<usize> for NonEmptyVec<T>
source§impl<T> IndexMut<&usize> for NonEmptyVec<T>
impl<T> IndexMut<&usize> for NonEmptyVec<T>
source§impl<T> IndexMut<usize> for NonEmptyVec<T>
impl<T> IndexMut<usize> for NonEmptyVec<T>
source§impl<'a, T> IntoIterator for &'a NonEmptyVec<T>
impl<'a, T> IntoIterator for &'a NonEmptyVec<T>
Allows for constructions on the form for t in ts.