Struct NonEmptyVec

Source
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>

Source

pub fn new(x: T) -> NonEmptyVec<T>

Source

pub fn first(&self) -> &T

Source

pub fn first_mut(&mut self) -> &mut T

Source

pub fn last(&self) -> &T

Returns a reference to the last element.

Source

pub fn last_mut(&mut self) -> &mut T

Returns a mutable reference to the last element.

Source

pub fn push(&mut self, x: T)

Append an element to the vector.

Source

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);
Source

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);
Source

pub fn is_empty(&self) -> bool

Always returns false.

Source

pub fn iter(&self) -> NonEmptyIter<'_, T>

Returns an iterator over the vector.

Trait Implementations§

Source§

impl<T: Clone> Clone for NonEmptyVec<T>

Source§

fn clone(&self) -> NonEmptyVec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Clone> From<&NonEmptyVec<T>> for Vec<T>

Source§

fn from(xs: &NonEmptyVec<T>) -> Vec<T>

Converts to this type from the input type.
Source§

impl<T> From<NonEmptyVec<T>> for Vec<T>

Source§

fn from(xs: NonEmptyVec<T>) -> Vec<T>

Converts to this type from the input type.
Source§

impl<T> Index<&usize> for NonEmptyVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: &usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<usize> for NonEmptyVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<&usize> for NonEmptyVec<T>

Source§

fn index_mut(&mut self, index: &usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for NonEmptyVec<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a NonEmptyVec<T>

Allows for constructions on the form for t in ts.

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = NonEmptyIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for NonEmptyVec<T>

Source§

fn eq(&self, other: &NonEmptyVec<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Clone> TryFrom<&[T]> for NonEmptyVec<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xs: &[T]) -> Result<NonEmptyVec<T>, Error>

Performs the conversion.
Source§

impl<T: Clone, const N: usize> TryFrom<&[T; N]> for NonEmptyVec<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xs: &[T; N]) -> Result<NonEmptyVec<T>, Error>

Performs the conversion.
Source§

impl<T: Clone> TryFrom<&Vec<T>> for NonEmptyVec<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xs: &Vec<T>) -> Result<NonEmptyVec<T>, Error>

Performs the conversion.
Source§

impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xs: Vec<T>) -> Result<NonEmptyVec<T>, Error>

Performs the conversion.
Source§

impl<T: Eq> Eq for NonEmptyVec<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V