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

Returns a reference to the last element.

Returns a mutable reference to the last element.

Append an element to the vector.

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

Returns the length of the vector.


let mut v = NonEmptyVec::new(1);
v.push(2);
v.push(3);
assert_eq!(v.len(), 3);

Always returns false.

Returns an iterator over the vector.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Converts to this type from the input type.

Converts to this type from the input type.

The returned type after indexing.

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

The returned type after indexing.

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

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

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

Allows for constructions on the form for t in ts.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.