docs

你正在阅读 v0.1.0 的文档。Nabi 0.1.0 已不再提供支持。 最新版本:v0.1.3 →

core::collections::list

本页内容

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.

  • pub const fn join(self, sep: str) -> str§

    Source

    The items with sep between each two: ["a", "b"].join(", ").

  • pub const fn new() -> List<T>§

    Source

    An empty list with room for 4 elements.

  • pub const fn with_capacity(cap: usize) -> List<T>§

    Source

    An empty list with room for cap elements.

  • pub const fn filled(n: usize, x: T) -> List<T>§

    Source

    n copies of x (a read buffer: List::filled(n, 0u8)).

  • pub const fn from_fn(n: usize, f: fn(usize) -> T) -> List<T>§

    Source

    n items, item i being f(i).

  • pub const fn len(self) -> usize§

    Source

    The number of elements.

  • pub const fn is_empty(self) -> bool§

    Source

    true when the list has no element.

  • pub const fn capacity(self) -> usize§

    Source

    Elements the list holds before it grows.

  • pub const fn reserve(self, additional: usize)§

    Source

    Room for additional more elements (doubling).

  • pub const fn shrink_to_fit(self)§

    Source

    Capacity cut down to the length.

  • pub const fn reverse(self)§

    Source

    The elements in reverse order, in place.

  • pub const fn extend<I: IntoIterator<Item = T>>(self, items: I)§

    Source

    Push every item of items, in order.

  • pub const fn push(self, x: T)§

    Source

    Append x at the end, growing the buffer as needed.

  • pub const fn pop(self) -> T | none§

    Source

    Remove and return the last element; none when empty.

  • pub const fn get(self, idx: usize) -> T | none§

    Source

    Element idx, or none out of bounds.

  • pub const fn first(self) -> T | none§

    Source

    The first element; none when empty.

  • pub const fn last(self) -> T | none§

    Source

    The last element; none when empty.

  • pub const fn insert(self, idx: usize, x: T)§

    Source

    Insert x at idx, shifting the later elements right; panics when idx > len().

  • pub const fn remove(self, idx: usize) -> T§

    Source

    Remove and return the element at idx, shifting the later ones left; panics when idx >= len().

  • pub const fn clear(self)§

    Source

    Remove every element; the capacity stays.

  • pub const fn truncate(self, len: usize)§

    Source

    Keep the first len elements (no change if there are fewer).

  • pub const fn drain(self, r: Range<usize>) -> List<T>§

    Source

    Remove the elements in r and return them, in order; the rest close up. Panics if r is reversed or ends past the length.

  • pub const unsafe fn set_len(self, n: usize)§

    Source

    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.

  • pub const fn iter(self) -> ListIter<T>§

    Source

    The elements front to back.

  • pub const fn retain(self, keep: fn(T) -> bool)§

    Source

    Keep only the elements keep accepts, in order, in place; O(n).

  • pub const fn fill(self, x: T)§

    Source

    Overwrite every element with x.

  • pub const fn swap(self, i: usize, j: usize)§

    Source

    Exchange elements i and j; panics out of bounds.

  • pub const fn split_at(self, mid: usize) -> (Slice<T>, Slice<T>)§

    Source

    Views of [0, mid) and [mid, len); panics if mid is past the length.

  • pub const fn windows(self, size: usize) -> slice::Windows<T>§

    Source

    Views of size consecutive elements, sliding by one (size > 0).

  • pub const fn chunks(self, size: usize) -> slice::Chunks<T>§

    Source

    Views of size elements in order, the last one shorter (size > 0).

  • pub const fn split_at_mut(self, mid: usize) -> (SliceMut<T>, SliceMut<T>)§

    Source

    Writable views of [0, mid) and [mid, len); panics if mid is past the length.

  • pub const fn chunks_mut(self, size: usize) -> slice::ChunksMut<T>§

    Source

    Writable views of size elements in order, the last one shorter (size > 0). They do not overlap, so par::for_each may give each to its own worker: par::for_each(xs.chunks_mut(n), |c| ..).

  • pub const fn freeze(self) -> FrozenList<T>§

    Source

    The list as a FrozenList, which threads share without a lock or a copy. O(1): the frozen list takes the elements over, and using self after is a compile error (to_list() gives a list to change again; freeze a clone() to keep this one).

  • pub const fn contains(self, x: T) -> bool§

    Source

    true when an element equals x.

  • pub const fn dedup(self)§

    Source

    Drop each element equal to the one before it, in place: a sorted list keeps one of each value.

  • pub const fn sort(self)§

    Source

    Ascending, stable.

  • Source

    The index of the first element equal to x in an ascending list, or none; O(log n).

  • pub const fn partition_point(self, pred: fn(T) -> bool) -> usize§

    Source

    The first index whose element fails pred, which must hold for a prefix and fail for the rest; O(log n). The insertion point of x in an ascending list is xs.partition_point(|e| e < x).

  • pub const fn sort_by_key<K: Ord>(self, key: fn(T) -> K)§

    Source

    Ascending by key(x), stable; key runs twice per comparison.

  • pub const fn sort_by(self, cmp: fn(T, T) -> core::cmp::Ordering)§

    Source

    Stable bottom-up merge sort by cmp: O(n log n), n extra slots.

  • pub const fn sort_unstable_by(self, cmp: fn(T, T) -> Ordering)§

    Source

    Ascending by cmp, in place, not stable (equal elements may change order); no extra memory and usually faster than sort_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.

    nabi
    let xs = [5, 3, 9, 1, 3];
    xs.sort_unstable_by(|a, b| b.cmp(a));
    assert(xs == [9, 5, 3, 3, 1]);
    
  • pub const fn sort_unstable_by_key<K: Ord>(self, key: fn(T) -> K)§

    Source

    Ascending by key(x), not stable; key runs twice per comparison.

  • pub const fn binary_search_by(self, f: fn(T) -> Ordering) -> usize | none§

    Source

    The index of an element f finds equal (f(x) is Equal) in a list f sees as ascending (Less before, Greater after), the first of them; none when there is none. O(log n).

    nabi
    let 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);
    
  • pub const fn binary_search_by_key<K: Ord>(self, k: K, key: fn(T) -> K) -> usize | none§

    Source

    binary_search_by comparing key(x) with k.

  • pub const fn rposition(self, pred: fn(T) -> bool) -> usize | none§

    Source

    The index of the last element pred accepts, or none.

    nabi
    let xs = [1, 4, 2, 4];
    assert(xs.rposition(|x| x == 4) == 3 && xs.rposition(|x| x > 9) is none);
    
  • pub const fn rotate_left(self, k: usize)§

    Source

    Shift every element k places toward the front, in place; the first k go to the back. Panics when k is past the length.

    nabi
    let 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]);
    
  • pub const fn rotate_right(self, k: usize)§

    Source

    Shift every element k places toward the back, in place; the last k go to the front. Panics when k is past the length.

  • pub const fn split_off(self, at: usize) -> List<T>§

    Source

    Remove the elements from at on and return them as a new list. Panics when at is past the length.

    nabi
    let 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]);
    
  • pub const fn dedup_by(self, same: fn(T, T) -> bool)§

    Source

    Drop each element same says matches the one kept before it, in place (same(kept, x)).

    nabi
    let 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"]);
    
  • pub const fn dedup_by_key<K: PartialEq>(self, key: fn(T) -> K)§

    Source

    Drop each element whose key equals the key of the one before.

  • pub const fn sort_unstable(self)§

    Source

    Ascending, in place, not stable: sort_unstable_by with cmp.

    nabi
    let xs = [3, 1, 2];
    xs.sort_unstable();
    assert(xs == [1, 2, 3]);
    
  • pub const fn concat(self) -> List<T>§

    Source

    The elements of every list in order, as one new list.

    nabi
    let rows = [[1, 2], [], [3]];
    assert(rows.concat() == [1, 2, 3]);
    
Trait implementations 16

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

Type aliases

ListIntoIterSource

pub type ListIntoIter<T> = ListIter<T>

What xs.into_iter() returns: the same walk as iter.