Struct std::collections::vector::Vector
Growable heap-allocated array.
Example
use std::collections::Vector;
let vec = Vector::new::<i32>();
defer vec.free();
vec.push(1);
vec.push(2);
vec.push(3);
assert_eq!(vec.len(), 3);
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
Run this example
Methods
impl Vector<T> { ... }
-
fn new() -> Vector<T>
Create an empty vector
This will not allocate until the first element is inserted.
-
Create a vector from a raw slice and a length.
The slice must be allocated with alloc and its length must be greater than or equal to
length
. -
Create a vector that can hold up to
capacity
elements without reallocating. -
fn from_slice(slice: &[T]) -> Vector<T>
Create a vector from a slice, copying the elements.
Example
use std::collections::Vector; let vec = Vector::from_slice(&[1, 2, 3]); assert_eq!(vec.len(), 3); assert_eq!(vec[..], &[1, 2, 3]);
Run this example -
Create a vector from an iterator.
See also to_vector for a chainable version.
Example
use std::collections::Vector; let vec: Vector<i32> = Vector::from_iter(&(0..3)); assert_eq!(vec.len(), 3); assert_eq!(vec[..], &[0, 1, 2]);
Run this example -
Reserve capacity for at least
additional
elements. -
fn extend_from_slice(self: &mut Vector<T>, value: &[T])
Extend the vector with the elements from a slice.
Example
use std::collections::Vector; let vec = Vector::new::<i32>(); vec.extend_from_slice(&[1, 2, 3]); vec.extend_from_slice(&[4, 5, 6]); assert_eq!(vec.len(), 6); assert_eq!(vec[..], &[1, 2, 3, 4, 5, 6]);
Run this example -
Extend the vector with the elements from an iterator.
-
Insert an element at the given index, shifting all elements after it.
Example
use std::collections::Vector; let vec = Vector::from_slice(&[0, 1, 2, 3, 4]); vec.insert(0, 100); vec.insert(4, 200); vec.insert(7, 300); assert_eq!(vec[..], &[100, 0, 1, 2, 200, 3, 4, 300]);
Run this example -
Get the element at the given index.
If the index is out of bounds, it returns
Option::none()
. -
fn as_slice(self: &Vector<T>) -> &[T]
-
fn as_slice_mut(self: &mut Vector<T>) -> &mut [T]
-
fn push(self: &mut Vector<T>, value: T)
Push an element to the end of the vector.
-
Pop an element from the end of the vector.
If the vector is empty, it returns
Option::none()
. -
Remove an element at the given index, shifting all elements after it.
-
Return the length of the vector.
-
Return the size of the underlying buffer.
-
fn spare_capacity(self: &mut Vector<T>) -> &mut [T]
Returns a region of memory "after the last element".
This can be used to efficiently append elements to the vector. The memory may not be initialized.
-
Returns
true
if the vector is empty,false
otherwise. -
fn clear(self: &mut Vector<T>)
Clear the vector, removing all elements.
Does not remove excess capacity, call shrink_to_fit afterwards, if this is desired.
-
Truncate the vector, removing all but the first
new_size
elements.new_size
should not be greater than the current capacity of the vector, but it may be greater than the current length of the vector. In this case the vector will grow to the specified capacity.vec.truncate(0)
is equivalent tovec.clear()
. -
Removes the elements not mathing the given predicate.
Does not remove excess capacity, call shrink_to_fit afterwards, if this is desired.
Example
let vec = (0..=10).to_vector(); defer vec.free(); vec.retain(|x: &i32| -> bool { *x % 2 == 0 }); assert_eq!(vec[..], &[0, 2, 4, 6, 8, 10]);
Run this example -
fn iter(self: &Vector<T>) -> SliceIterator<&T>
-
fn iter_ref(self: &Vector<T>) -> SliceRefIterator<&T>
-
fn iter_mut(self: &mut Vector<T>) -> SliceRefIterator<&mut T>
Returns an iterator over mutable pointers to the elements of the collection.
See IterableMut for details.
-
fn borrow(self: &Vector<T>) -> &[T]
Returns a view to the object.
-
fn borrow_mut(self: &mut Vector<T>) -> &mut [T]
Returns a mutable view to the object.
-
fn shrink_to_fit(self: &mut Vector<T>)
Shrink the underlying buffer to the length of the vector.
-
fn free(self: &mut Vector<T>)
Frees the memory backing the object.
-
fn free_all(self: &mut Vector<T>)
Helper for collections that own heap-allocated objects. This is used to be able to do
defer col.free_all()
without much boilerplate.Example
use std::collections::Vector; use std::string::StringBuf; use std::fmt::format; let v: Vector<StringBuf> = Vector::new(); defer v.free_all(); for i in 1..10 { v.push("{} + {} = {}".format!(i, i, i + i).unwrap()); }
Run this example -
Returns a copy of the object.