Movatterモバイル変換


[0]ホーム

URL:


Module vec

std

Modulevec 

1.0.0 ·Source
Expand description

A contiguous growable array type with heap-allocated contents, writtenVec<T>.

Vectors haveO(1) indexing, amortizedO(1) push (to the end) andO(1) pop (from the end).

Vectors ensure they never allocate more thanisize::MAX bytes.

§Examples

You can explicitly create aVec withVec::new:

letv: Vec<i32> = Vec::new();

…or by using thevec! macro:

letv: Vec<i32> =vec![];letv =vec![1,2,3,4,5];letv =vec![0;10];// ten zeroes

You canpush values onto the end of a vector (which will grow the vectoras needed):

letmutv =vec![1,2];v.push(3);

Popping values works in much the same way:

letmutv =vec![1,2];lettwo = v.pop();

Vectors also support indexing (through theIndex andIndexMut traits):

letmutv =vec![1,2,3];letthree = v[2];v[1] = v[1] +5;

§Memory layout

When the type is non-zero-sized and the capacity is nonzero,Vec uses theGlobalallocator for its allocation. It is valid to convert both ways between such aVec and a rawpointer allocated with theGlobal allocator, provided that theLayout used with theallocator is correct for a sequence ofcapacity elements of the type, and the firstlenvalues pointed to by the raw pointer are valid. More precisely, aptr: *mut T that has beenallocated with theGlobal allocator withLayout::array::<T>(capacity) maybe converted into a vec usingVec::<T>::from_raw_parts(ptr, len, capacity). Conversely, the memorybacking avalue: *mut T obtained fromVec::<T>::as_mut_ptr may be deallocated using theGlobal allocator with the same layout.

For zero-sized types (ZSTs), or when the capacity is zero, theVec pointer must be non-nulland sufficiently aligned. The recommended way to build aVec of ZSTs ifvec! cannot beused is to useptr::NonNull::dangling.

Structs§

Drain
A draining iterator forVec<T>.
ExtractIf
An iterator which uses a closure to determine if an element should be removed.
IntoIter
An iterator that moves out of a vector.
Splice
A splicing iterator forVec.
Vec
A contiguous growable array type, written asVec<T>, short for ‘vector’.
PeekMutExperimental
Structure wrapping a mutable reference to the last item in aVec.

[8]ページ先頭

©2009-2026 Movatter.jp