core::collections::list
本頁內容
- Structs
List52binary_searchbinary_search_bybinary_search_by_keycapacitychunkschunks_mutclearconcatcontainsdedupdedup_bydedup_by_keydrainextendfillfilledfirstfreezefrom_fngetinsertis_emptyiterjoinlastlennewpartition_pointpoppushremovereserveretainreverserotate_leftrotate_rightrpositionset_lenshrink_to_fitsortsort_bysort_by_keysort_unstablesort_unstable_bysort_unstable_by_keysplit_atsplit_at_mutsplit_offswaptruncatewindowswith_capacity
- Type aliases
API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。
List<T>, a growable array: one
object holding the length, the capacity and the current element
buffer, swapped for a bigger one on growth. The buffer has one slot
past the capacity that is never written: a zero T that vacated
slots are overwritten with, so the list keeps no removed value alive.
Structs
ListSource
pub struct List<T>A growable array; copies share one buffer.
The items with
sepbetween each two:["a", "b"].join(", ").An empty list with room for 4 elements.
An empty list with room for
capelements.ncopies ofx(a read buffer:List::filled(n, 0u8)).nitems, itemibeingf(i).The number of elements.
truewhen the list has no element.Elements the list holds before it grows.
Room for
additionalmore elements (doubling).Capacity cut down to the length.
The elements in reverse order, in place.
Sourcepub const fn extend<I: IntoIterator<Item = T>>(§self, items: I) Push every item of
items, in order.Append
xat the end, growing the buffer as needed.Remove and return the last element;
nonewhen empty.Element
idx, ornoneout of bounds.The first element;
nonewhen empty.The last element;
nonewhen empty.Insert
xatidx, shifting the later elements right; panics whenidx > len().Remove and return the element at
idx, shifting the later ones left; panics whenidx >= len().Remove every element; the capacity stays.
Keep the first
lenelements (no change if there are fewer).Remove the elements in
rand return them, in order; the rest close up. Panics ifris reversed or ends past the length.Set the length to
n(at most the capacity) without writing elements, for a buffer filled in place: elements past the old length hold whatever the storage held (zero: vacated slots are zeroed). Shrinking this way leaves the cut elements in place.The elements front to back.
Keep only the elements
keepaccepts, in order, in place; O(n).Overwrite every element with
x.Exchange elements
iandj; panics out of bounds.Views of
[0, mid)and[mid, len); panics ifmidis past the length.Views of
sizeconsecutive elements, sliding by one (size> 0).Views of
sizeelements in order, the last one shorter (size> 0).Writable views of
[0, mid)and[mid, len); panics ifmidis past the length.Writable views of
sizeelements in order, the last one shorter (size> 0). They do not overlap, sopar::for_eachmay give each to its own worker:par::for_each(xs.chunks_mut(n), |c| ..).
Sourcepub const fn freeze(§self) -> FrozenList<T> The list as a
FrozenList, which threads share without a lock or a copy. O(1): the frozen list takes the elements over, and usingselfafter is a compile error (to_list()gives a list to change again; freeze aclone()to keep this one).truewhen an element equalsx.Drop each element equal to the one before it, in place: a sorted list keeps one of each value.
Ascending, stable.
The index of the first element equal to
xin an ascending list, ornone; O(log n).The first index whose element fails
pred, which must hold for a prefix and fail for the rest; O(log n). The insertion point ofxin an ascending list isxs.partition_point(|e| e < x).Ascending by
key(x), stable;keyruns twice per comparison.Stable bottom-up merge sort by
cmp: O(n log n), n extra slots.Ascending by
cmp, in place, not stable (equal elements may change order); no extra memory and usually faster thansort_by. Quicksort with a median-of-three pivot, insertion sort for small parts and heapsort past a depth of 2 log2(n), so O(n log n) always.nabilet xs = [5, 3, 9, 1, 3]; xs.sort_unstable_by(|a, b| b.cmp(a)); assert(xs == [9, 5, 3, 3, 1]);Ascending by
key(x), not stable;keyruns twice per comparison.The index of an element
ffinds equal (f(x)isEqual) in a listfsees as ascending (Lessbefore,Greaterafter), the first of them;nonewhen there is none. O(log n).nabilet xs = [(1, "a"), (3, "c"), (5, "e")]; assert(xs.binary_search_by(|p| p.0.cmp(3)) == 1); assert(xs.binary_search_by_key(5, |p| p.0) == 2); assert(xs.binary_search_by_key(4, |p| p.0) is none);binary_search_bycomparingkey(x)withk.The index of the last element
predaccepts, ornone.nabilet xs = [1, 4, 2, 4]; assert(xs.rposition(|x| x == 4) == 3 && xs.rposition(|x| x > 9) is none);Shift every element
kplaces toward the front, in place; the firstkgo to the back. Panics whenkis past the length.nabilet xs = [1, 2, 3, 4, 5]; xs.rotate_left(2); assert(xs == [3, 4, 5, 1, 2]); xs.rotate_right(2); assert(xs == [1, 2, 3, 4, 5]);Shift every element
kplaces toward the back, in place; the lastkgo to the front. Panics whenkis past the length.Remove the elements from
aton and return them as a new list. Panics whenatis past the length.nabilet xs = [1, 2, 3, 4]; let tail = xs.split_off(1); assert(xs == [1] && tail == [2, 3, 4]); xs += tail; assert(xs == [1, 2, 3, 4]);Drop each element
samesays matches the one kept before it, in place (same(kept, x)).nabilet xs = [1, 2, 4, 5, 9]; xs.dedup_by(|a, b| b - a == 1); assert(xs == [1, 4, 9]); let ws = ["apple", "avocado", "banana"]; ws.dedup_by_key(|w| w.chars().next()); assert(ws == ["apple", "banana"]);Drop each element whose
keyequals the key of the one before.Ascending, in place, not stable:
sort_unstable_bywithcmp.nabilet xs = [3, 1, 2]; xs.sort_unstable(); assert(xs == [1, 2, 3]);The elements of every list in order, as one new list.
nabilet rows = [[1, 2], [], [3]]; assert(rows.concat() == [1, 2, 3]);
Trait implementations 16
impl<T> !Sync for List<T>impl<T> IntoIterator for List<T>impl<T> Default for List<T>impl<T: Clone> Clone for List<T>impl<T> Index<usize> for List<T>impl<T> IndexSet<usize, T> for List<T>impl<T> FromIterator<T> for List<T>impl<T: PartialEq> PartialEq for List<T>impl<T: Eq> Eq for List<T>impl<T: PartialEq + PartialOrd> PartialOrd for List<T>impl<T: Ord> Ord for List<T>impl<T: Hash> Hash for List<T>impl<T: Debug> Debug for List<T>impl<T> AddAssign<T> for List<T>impl<T> AddAssign<List<T>> for List<T>impl<T> Transfer for List<T>
ListIterSource
pub struct ListIter<T>xs.iter() / for x in xs: walks a list front to back. Its state
is its own (next(ref self)): a copy walks on its own.
Trait implementations 4
impl<T> Debug for ListIter<T>impl<T> Iterator for ListIter<T>impl<T> DoubleEndedIterator for ListIter<T>impl<T> ExactSizeIterator for ListIter<T>
Type aliases
ListIntoIterSource
pub type ListIntoIter<T> = ListIter<T>What xs.into_iter() returns: the same walk as iter.