# [−][src]Struct heapless::Vec

`pub struct Vec<T, N>(_) where    N: ArrayLength<T>;`

A fixed capacity `Vec`

# Examples

```use heapless::Vec;
use heapless::consts::*;

// A vector with a fixed capacity of 8 elements allocated on the stack
let mut vec = Vec::<_, U8>::new();
vec.push(1);
vec.push(2);

assert_eq!(vec.len(), 2);
assert_eq!(vec[0], 1);

assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.len(), 1);

vec[0] = 7;
assert_eq!(vec[0], 7);

vec.extend([1, 2, 3].iter().cloned());

for x in &vec {
println!("{}", x);
}
assert_eq!(vec, [7, 1, 2, 3]);```

## Methods

### `impl<T, N> Vec<T, N> where    N: ArrayLength<T>, `[src]

#### `pub fn new() -> Self`[src]

Constructs a new, empty vector with a fixed capacity of `N`

# Examples

```use heapless::Vec;
use heapless::consts::*;

// allocate the vector on the stack
let mut x: Vec<u8, U16> = Vec::new();

// allocate the vector in a static variable
static mut X: Vec<u8, U16> = Vec(heapless::i::Vec::new());```

#### `pub fn from_slice(other: &[T]) -> Result<Self, ()> where    T: Clone, `[src]

Constructs a new vector with a fixed capacity of `N` and fills it with the provided slice.

This is equivalent to the following code:

```use heapless::Vec;
use heapless::consts::*;

let mut v: Vec<u8, U16> = Vec::new();
v.extend_from_slice(&[1, 2, 3]).unwrap();```

#### `pub fn capacity(&self) -> usize`[src]

Returns the maximum number of elements the vector can hold

#### `pub fn clear(&mut self)`[src]

Clears the vector, removing all values.

#### `pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()> where    T: Clone, `[src]

Clones and appends all elements in a slice to the `Vec`.

Iterates over the slice `other`, clones each element, and then appends it to this `Vec`. The `other` vector is traversed in-order.

# Examples

```use heapless::Vec;
use heapless::consts::*;

let mut vec = Vec::<u8, U8>::new();
vec.push(1).unwrap();
vec.extend_from_slice(&[2, 3, 4]).unwrap();
assert_eq!(*vec, [1, 2, 3, 4]);```

#### `pub fn pop(&mut self) -> Option<T>`[src]

Removes the last element from a vector and return it, or `None` if it's empty

#### `pub fn push(&mut self, item: T) -> Result<(), T>`[src]

Appends an `item` to the back of the collection

Returns back the `item` if the vector is full

#### `pub fn truncate(&mut self, len: usize)`[src]

Shortens the vector, keeping the first `len` elements and dropping the rest.

#### `pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()> where    T: Clone, `[src]

Resizes the Vec in-place so that len is equal to new_len.

If new_len is greater than len, the Vec is extended by the difference, with each additional slot filled with value. If new_len is less than len, the Vec is simply truncated.

See also `resize_default`.

#### `pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()> where    T: Clone + Default, `[src]

Resizes the `Vec` in-place so that `len` is equal to `new_len`.

If `new_len` is greater than `len`, the `Vec` is extended by the difference, with each additional slot filled with `Default::default()`. If `new_len` is less than `len`, the `Vec` is simply truncated.

See also `resize`.

#### `pub fn swap_remove(&mut self, index: usize) -> T`[src]

Removes an element from the vector and returns it.

The removed element is replaced by the last element of the vector.

This does not preserve ordering, but is O(1).

# Panics

Panics if `index` is out of bounds.

# Examples

```use heapless::Vec;
use heapless::consts::*;

let mut v: Vec<_, U8> = Vec::new();
v.push("foo").unwrap();
v.push("bar").unwrap();
v.push("baz").unwrap();
v.push("qux").unwrap();

assert_eq!(v.swap_remove(1), "bar");
assert_eq!(&*v, ["foo", "qux", "baz"]);

assert_eq!(v.swap_remove(0), "foo");
assert_eq!(&*v, ["baz", "qux"]);```

#### `pub fn starts_with(&self, needle: &[T]) -> bool where    T: PartialEq, `[src]

Returns `true` if `needle` is a prefix of the Vec.

Always returns `true` if `needle` is an empty slice.

# Examples

```use heapless::Vec;
use heapless::consts::*;

let v: Vec<_, U8> = Vec::from_slice(b"abc").unwrap();
assert_eq!(v.starts_with(b""), true);
assert_eq!(v.starts_with(b"ab"), true);
assert_eq!(v.starts_with(b"bc"), false);```

#### `pub fn ends_with(&self, needle: &[T]) -> bool where    T: PartialEq, `[src]

Returns `true` if `needle` is a suffix of the Vec.

Always returns `true` if `needle` is an empty slice.

# Examples

```use heapless::Vec;
use heapless::consts::*;

let v: Vec<_, U8> = Vec::from_slice(b"abc").unwrap();
assert_eq!(v.ends_with(b""), true);
assert_eq!(v.ends_with(b"ab"), false);
assert_eq!(v.ends_with(b"bc"), true);```

## Trait Implementations

### `impl<T, N> Deref for Vec<T, N> where    N: ArrayLength<T>, `[src]

#### `type Target = [T]`

The resulting type after dereferencing.

### `impl<'a, T, N> IntoIterator for &'a Vec<T, N> where    N: ArrayLength<T>, `[src]

#### `type Item = &'a T`

The type of the elements being iterated over.

#### `type IntoIter = Iter<'a, T>`

Which kind of iterator are we turning this into?

### `impl<'a, T, N> IntoIterator for &'a mut Vec<T, N> where    N: ArrayLength<T>, `[src]

#### `type Item = &'a mut T`

The type of the elements being iterated over.

#### `type IntoIter = IterMut<'a, T>`

Which kind of iterator are we turning this into?

### `impl<T, N> IntoIterator for Vec<T, N> where    N: ArrayLength<T>, `[src]

#### `type Item = T`

The type of the elements being iterated over.

#### `type IntoIter = IntoIter<T, N>`

Which kind of iterator are we turning this into?

## Blanket Implementations

### `impl<I> IntoIterator for I where    I: Iterator, `[src]

#### `type Item = <I as Iterator>::Item`

The type of the elements being iterated over.

#### `type IntoIter = I`

Which kind of iterator are we turning this into?

### `impl<T> Same<T> for T`

#### `type Output = T`

Should always be `Self`

### `impl<T, U> TryFrom<U> for T where    U: Into<T>, `[src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

### `impl<T, U> TryInto<U> for T where    U: TryFrom<T>, `[src]

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

The type returned in the event of a conversion error.