extend_one)extend_one)container[index]) operation. Read morecontainer[index]) operation. Read moreConstruct an empty vector.
\nThis is a const version of [SmallVec::new] that is enabled by the feature const_new, with the limitation that it only works for arrays.
The array passed as an argument is moved to be an inline version of SmallVec.
This is a const version of [SmallVec::from_buf] that is enabled by the feature const_new, with the limitation that it only works for arrays.
Constructs a new SmallVec on the stack from an array without\ncopying elements. Also sets the length. The user is responsible\nfor ensuring that len <= N.
This is a const version of [SmallVec::from_buf_and_len_unchecked] that is enabled by the feature const_new, with the limitation that it only works for arrays.
Construct an empty vector with enough capacity pre-allocated to store at least n\nelements.
Will create a heap allocation only if n is larger than the inline capacity.
\nlet v: SmallVec<[u8; 3]> = SmallVec::with_capacity(100);\n\nassert!(v.is_empty());\nassert!(v.capacity() >= 100);Construct a new SmallVec from a Vec<A::Item>.
Elements will be copied to the inline buffer if vec.capacity() <= Self::inline_capacity().
use smallvec::SmallVec;\n\nlet vec = vec![1, 2, 3, 4, 5];\nlet small_vec: SmallVec<[_; 3]> = SmallVec::from_vec(vec);\n\nassert_eq!(&*small_vec, &[1, 2, 3, 4, 5]);Constructs a new SmallVec on the stack from an A without\ncopying elements.
use smallvec::SmallVec;\n\nlet buf = [1, 2, 3, 4, 5];\nlet small_vec: SmallVec<_> = SmallVec::from_buf(buf);\n\nassert_eq!(&*small_vec, &[1, 2, 3, 4, 5]);Constructs a new SmallVec on the stack from an A without\ncopying elements. Also sets the length, which must be less or\nequal to the size of buf.
use smallvec::SmallVec;\n\nlet buf = [1, 2, 3, 4, 5, 0, 0, 0];\nlet small_vec: SmallVec<_> = SmallVec::from_buf_and_len(buf, 5);\n\nassert_eq!(&*small_vec, &[1, 2, 3, 4, 5]);Constructs a new SmallVec on the stack from an A without\ncopying elements. Also sets the length. The user is responsible\nfor ensuring that len <= A::size().
use smallvec::SmallVec;\nuse std::mem::MaybeUninit;\n\nlet buf = [1, 2, 3, 4, 5, 0, 0, 0];\nlet small_vec: SmallVec<_> = unsafe {\n SmallVec::from_buf_and_len_unchecked(MaybeUninit::new(buf), 5)\n};\n\nassert_eq!(&*small_vec, &[1, 2, 3, 4, 5]);Sets the length of a vector.
\nThis will explicitly set the size of the vector, without actually\nmodifying its buffers, so it is up to the caller to ensure that the\nvector is actually the specified size.
\nThe maximum number of elements this vector can hold inline
\nReturns true if the data has spilled into a separate heap-allocated buffer.
Creates a draining iterator that removes the specified range in the vector\nand yields the removed items.
\nNote 1: The element range is removed even if the iterator is only\npartially consumed or not consumed at all.
\nNote 2: It is unspecified how many elements are removed from the vector\nif the Drain value is leaked.
Panics if the starting point is greater than the end point or if\nthe end point is greater than the length of the vector.
\nAppend an item to the vector.
\nRemove an item from the end of the vector and return it, or None if empty.
\nMoves all the elements of other into self, leaving other empty.
let mut v0: SmallVec<[u8; 16]> = smallvec![1, 2, 3];\nlet mut v1: SmallVec<[u8; 32]> = smallvec![4, 5, 6];\nv0.append(&mut v1);\nassert_eq!(*v0, [1, 2, 3, 4, 5, 6]);\nassert_eq!(*v1, []);Re-allocate to set the capacity to max(new_cap, inline_size()).
Panics if new_cap is less than the vector’s length\nor if the capacity computation overflows usize.
Re-allocate to set the capacity to max(new_cap, inline_size()).
Panics if new_cap is less than the vector’s length
Reserve capacity for additional more elements to be inserted.
May reserve more space to avoid frequent reallocations.
\nPanics if the capacity computation overflows usize.
Reserve capacity for additional more elements to be inserted.
May reserve more space to avoid frequent reallocations.
\nReserve the minimum capacity for additional more elements to be inserted.
Panics if the new capacity overflows usize.
Reserve the minimum capacity for additional more elements to be inserted.
Shrink the capacity of the vector as much as possible.
\nWhen possible, this will move data from an external heap buffer to the vector’s inline\nstorage.
\nShorten the vector, keeping the first len elements and dropping the rest.
If len is greater than or equal to the vector’s current length, this has no\neffect.
This does not re-allocate. If you want the vector’s capacity to shrink, call\nshrink_to_fit after truncating.
Extracts a slice containing the entire vector.
\nEquivalent to &s[..].
Extracts a mutable slice of the entire vector.
\nEquivalent to &mut s[..].
Remove the element at position index, replacing it with the last element.
This does not preserve ordering, but is O(1).
\nPanics if index is out of bounds.
Remove all elements from the vector.
\nRemove and return the element at position index, shifting all elements after it to the\nleft.
Panics if index is out of bounds.
Insert an element at position index, shifting all elements after it to the right.
Panics if index > len.
Insert multiple elements at position index, shifting all following elements toward the\nback.
Convert a SmallVec to a Vec, without reallocating if the SmallVec has already spilled onto\nthe heap.
Converts a SmallVec into a Box<[T]> without reallocating if the SmallVec has already spilled\nonto the heap.
Note that this will drop any excess capacity.
\nConvert the SmallVec into an A if possible. Otherwise return Err(Self).
This method returns Err(Self) if the SmallVec is too short (and the A contains uninitialized elements),\nor if the SmallVec is too long (and all the elements were spilled to the heap).
Retains only the elements specified by the predicate.
\nIn other words, remove all elements e such that f(&e) returns false.\nThis method operates in place and preserves the order of the retained\nelements.
Retains only the elements specified by the predicate.
\nThis method is identical in behaviour to [retain]; it is included only\nto maintain api-compatibility with std::Vec, where the methods are\nseparate for historical reasons.
Removes consecutive duplicate elements.
\nRemoves consecutive duplicate elements using the given equality relation.
\nRemoves consecutive elements that map to the same key.
\nResizes the SmallVec in-place so that len is equal to new_len.
If new_len is greater than len, the SmallVec is extended by the difference, with each\nadditional slot filled with the result of calling the closure f. The return values from f\nwill end up in the SmallVec in the order they have been generated.
If new_len is less than len, the SmallVec is simply truncated.
This method uses a closure to create new values on every push. If you’d rather Clone a given\nvalue, use resize. If you want to use the Default trait to generate values, you can pass\nDefault::default() as the second argument.
Added for std::vec::Vec compatibility (added in Rust 1.33.0)
let mut vec : SmallVec<[_; 4]> = smallvec![1, 2, 3];\nvec.resize_with(5, Default::default);\nassert_eq!(&*vec, &[1, 2, 3, 0, 0]);\n\nlet mut vec : SmallVec<[_; 4]> = smallvec![];\nlet mut p = 1;\nvec.resize_with(4, || { p *= 2; p });\nassert_eq!(&*vec, &[2, 4, 8, 16]);Creates a SmallVec directly from the raw components of another\nSmallVec.
This is highly unsafe, due to the number of invariants that aren’t\nchecked:
\nptr needs to have been previously allocated via SmallVec for its\nspilled storage (at least, it’s highly likely to be incorrect if it\nwasn’t).ptr’s A::Item type needs to be the same size and alignment that\nit was allocated withlength needs to be less than or equal to capacity.capacity needs to be the capacity that the pointer was allocated\nwith.Violating these may cause problems like corrupting the allocator’s\ninternal data structures.
\nAdditionally, capacity must be greater than the amount of inline\nstorage A has; that is, the new SmallVec must need to spill over\ninto heap allocated storage. This condition is asserted against.
The ownership of ptr is effectively transferred to the\nSmallVec which may then deallocate, reallocate or change the\ncontents of memory pointed to by the pointer at will. Ensure\nthat nothing else uses the pointer after calling this\nfunction.
use std::mem;\nuse std::ptr;\n\nfn main() {\n let mut v: SmallVec<[_; 1]> = smallvec![1, 2, 3];\n\n // Pull out the important parts of `v`.\n let p = v.as_mut_ptr();\n let len = v.len();\n let cap = v.capacity();\n let spilled = v.spilled();\n\n unsafe {\n // Forget all about `v`. The heap allocation that stored the\n // three values won't be deallocated.\n mem::forget(v);\n\n // Overwrite memory with [4, 5, 6].\n //\n // This is only safe if `spilled` is true! Otherwise, we are\n // writing into the old `SmallVec`'s inline storage on the\n // stack.\n assert!(spilled);\n for i in 0..len {\n ptr::write(p.add(i), 4 + i);\n }\n\n // Put everything back together into a SmallVec with a different\n // amount of inline storage, but which is still less than `cap`.\n let rebuilt = SmallVec::<[_; 2]>::from_raw_parts(p, len, cap);\n assert_eq!(&*rebuilt, &[4, 5, 6]);\n }\n}Returns a raw mutable pointer to the vector’s buffer.
\nCopy the elements from a slice into a new SmallVec.
For slices of Copy types, this is more efficient than SmallVec::from(slice).
Copy elements from a slice into the vector at position index, shifting any following\nelements toward the back.
For slices of Copy types, this is more efficient than insert.
Copy elements from a slice and append them to the vector.
\nFor slices of Copy types, this is more efficient than extend.
can_vector)write_all_vectored)