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 duplicate 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: Clone> From<&NonEmptyVec<T>> for Vec<T>
impl<T: Clone> From<&NonEmptyVec<T>> for Vec<T>
Source§fn from(xs: &NonEmptyVec<T>) -> Vec<T>
fn from(xs: &NonEmptyVec<T>) -> Vec<T>
Converts to this type from the input type.
Source§impl<T> From<NonEmptyVec<T>> for Vec<T>
impl<T> From<NonEmptyVec<T>> for Vec<T>
Source§fn from(xs: NonEmptyVec<T>) -> Vec<T>
fn from(xs: NonEmptyVec<T>) -> Vec<T>
Converts to this type from the input type.
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>
Allows for constructions on the form for t in ts.
impl<'a, T> IntoIterator for &'a NonEmptyVec<T>
Allows for constructions on the form for t in ts.
Source§impl<T: PartialEq> PartialEq for NonEmptyVec<T>
impl<T: PartialEq> PartialEq for NonEmptyVec<T>
Source§impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>
impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>
impl<T: Eq> Eq for NonEmptyVec<T>
impl<T> StructuralPartialEq for NonEmptyVec<T>
Auto Trait Implementations§
impl<T> Freeze for NonEmptyVec<T>where
T: Freeze,
impl<T> RefUnwindSafe for NonEmptyVec<T>where
T: RefUnwindSafe,
impl<T> Send for NonEmptyVec<T>where
T: Send,
impl<T> Sync for NonEmptyVec<T>where
T: Sync,
impl<T> Unpin for NonEmptyVec<T>where
T: Unpin,
impl<T> UnwindSafe for NonEmptyVec<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more