A dynamic vector.
This is the type that is constructed in rune when an array expression such as [1, 2, 3]
is used.
Comparisons
Shorter sequences are considered smaller than longer ones, and vice versa.
assert!;
assert!;
assert!;
Methods
Constructs a new, empty dynamic Vec
.
The vector will not allocate until elements are pushed onto it.
Examples
let vec = Vec new;
Constructs a new, empty dynamic Vec
with at least the specified capacity.
The vector will be able to hold at least capacity
elements without reallocating. This method is allowed to allocate for more elements than capacity
. If capacity
is 0, the vector will not allocate.
It is important to note that although the returned vector has the minimum capacity specified, the vector will have a zero length. For an explanation of the difference between length and capacity, see Capacity and reallocation.
If it is important to know the exact allocated capacity of a Vec
, always use the capacity
method after construction.
Panics
Panics if the new capacity exceeds isize::MAX
bytes.
Examples
let vec = Vec with_capacity;
// The vector contains no items, even though it has capacity for more
assert_eq!;
assert!;
// These are all done without reallocating...
for i in 0..10
assert_eq!;
assert!;
// ...but this may make the vector reallocate
vec.push;
assert_eq!;
assert!;
Returns the number of elements in the vector, also referred to as its 'length'.
Examples
let a = ;
assert_eq!;
Returns true
if the vector contains no elements.
Examples
let v = Vec new;
assert!;
v.push;
assert!;
Returns the total number of elements the vector can hold without reallocating.
Examples
let vec = Vec with_capacity;
vec.push;
assert!;
Returns a reference to an element or subslice depending on the type of index.
- If given a position, returns a reference to the element at that position or
None
if out of bounds. - If given a range, returns the subslice corresponding to that range, or
None
if out of bounds.
Examples
let v = ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Clears the vector, removing all values.
Note that this method has no effect on the allocated capacity of the vector.
Examples
let v = ;
v.clear;
assert!;
Extend these bytes with another collection.
Examples
let vec = ;
vec.extend;
assert_eq!;
Iterate over the vector.
Examples
let vec = ;
let it = vec.iter;
assert_eq!;
assert_eq!;
Removes the last element from a vector and returns it, or [None
] if it is empty.
If you'd like to pop the first element, consider using VecDeque::pop_front
instead.
Examples
let vec = ;
assert_eq!;
assert_eq!;
Appends an element to the back of a collection.
Panics
Panics if the new capacity exceeds isize::MAX
bytes.
Examples
let vec = ;
vec.push;
assert_eq!;
Removes and returns the element at position index
within the vector, shifting all elements after it to the left.
Note: Because this shifts over the remaining elements, it has a worst-case performance of O(n). If you don't need the order of elements to be preserved, use swap_remove
instead. If you'd like to remove elements from the beginning of the Vec
, consider using VecDeque::pop_front
instead.
Panics
Panics if index
is out of bounds.
let v = ;
v.remove;
Examples
let v = ;
assert_eq!;
assert_eq!;
Inserts an element at position index
within the vector, shifting all elements after it to the right.
Panics
Panics if index > len
.
Examples
let vec = ;
vec.insert;
assert_eq!;
vec.insert;
assert_eq!;
Sort a vector by the specified comparator function.
Examples
use cmp;
let values = ;
values.sort_by
Sort the vector.
This require all elements to be of the same type, and implement total ordering per the [CMP
] protocol.
Panics
If any elements present are not comparable, this method will panic.
This will panic because a tuple and a string are not comparable:
let values = ;
values.sort;
This too will panic because floating point values which do not have a total ordering:
let values = ;
values.sort;
Examples
let values = ;
values.sort;
assert_eq!;
let values = ;
values.sort;
assert_eq!;
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.
This method requires T
to implement [Clone
], in order to be able to clone the passed value. If you need more flexibility (or want to rely on [Default
] instead of [Clone
]), use [Vec::resize_with
]. If you only need to resize to a smaller size, use [Vec::truncate
].
Examples
let vec = ;
vec.resize;
assert_eq!;
let vec = ;
vec.resize;
assert_eq!;
Resizing calls CLONE
each new element, which means they are not structurally shared:
let inner = ;
let vec = ;
vec.resize;
inner.push;
vec .push;
assert_eq!;
Trait Implementations
Clone the specified value
.
Examples
let a = 42;
let b = a;
let c = a.clone;
a += 1;
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for inequality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than (for self
and other
) and is used by the <
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than or equal to (for self
and other
) and is used by the <=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than (for self
and other
) and is used by the >
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than or equal to (for self
and other
) and is used by the >=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Return the minimum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Return the maximum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Protocols
for item in value
Construct an iterator over the tuple.
Examples
let vec = ;
let out = ;
for v in vec
assert_eq!;
let $out = value
Returns a reference to an element or subslice depending on the type of index.
- If given a position, returns a reference to the element at that position or
None
if out of bounds. - If given a range, returns the subslice corresponding to that range, or
None
if out of bounds.
Panics
Panics if the specified index
is out of range.
let v = ;
assert_eq!;
let v = ;
assert_eq!;
Examples
let v = ;
assert_eq!;
assert_eq!;
value= $input
Inserts a value into the vector.
Examples
let vec = ;
vec = "a";
assert_eq!;
format!
Write a debug representation to a string.
This calls the [DEBUG_FMT
] protocol over all elements of the collection.
Examples
let vec = ;
assert_eq!;
let $out = clone
Clone the vector.
Examples
let a = ;
let b = a.clone;
b.push;
assert_eq!;
assert_eq!;
if value == b
Perform a partial equality check with this vector.
This can take any argument which can be converted into an iterator using [INTO_ITER
].
Examples
let vec = ;
assert!;
assert!;
assert!;
if value == b
Perform a total equality check with this vector.
Examples
use eq;
let vec = ;
assert!;
assert!;
if value < b
Perform a partial comparison check with this vector.
Examples
let vec = ;
assert!;
assert!;
if value < b
Perform a total comparison check with this vector.
Examples
use Ordering;
use cmp;
let vec = ;
assert_eq!;
assert_eq!;