docs

您正在閱讀 v0.1.2 的文件。Nabi 0.1.2 已停止支援。 最新版本:v0.1.3 →

core::collections::slice

本頁內容

API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。

Slice<T>, a zero-copy view; element i is buf[start + i * stride], and the view keeps its buffer alive.

xs[a:b] is xs.slice(a..b) and xs[a:b:k] is xs.slice(index::slice_range(a, b, k, len)).step(k), with Python's semantics (bounds clamp, xs[::-1] reverses). A view of a List sees later element writes but not a regrowth, which moves the list to a new buffer; a view of an array or StrBuf views a copy.

Structs

SliceSource

pub struct Slice<T>

A zero-copy view of a buffer: element i is buf[start + i * stride]. A view of a List sees later element writes but not a regrowth; a view of an array or StrBuf views a copy.

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

    Source

    The number of elements.

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

    Source

    true when the slice has no element.

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

    Source

    Element i, 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 step(self, k: isize) -> Slice<T>§

    Source

    Every kth element (k != 0), back to front when k < 0.

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

    Source

    The elements front to back.

  • 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) -> Windows<T>§

    Source

    Views of size consecutive elements, sliding by one (size > 0); nothing when the view is shorter than size.

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

    Source

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

  • pub const unsafe fn raw_parts(self) -> (GcBuf<T>, usize, usize)§

    Source

    (buffer, start, len) of the viewed elements as one contiguous run, for runtime calls on a buffer region. A strided view is copied first, so writes through its run do not reach the view.

  • pub const unsafe fn from_raw_parts(buf: GcBuf<T>, start: usize, len: usize) -> Slice<T>§

    Source

    The view raw_parts takes apart: len elements of buf from start.

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

    Source

    A new list holding the viewed elements.

  • 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).

  • Source

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

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

    Source

    true when an element equals x.

  • pub const fn find<B: AsBytes>(self, needle: B) -> usize | none§

    Source
Trait implementations 8

SliceIterSource

pub struct SliceIter<T>

s.iter() / for x in s, in view order.

Trait implementations 4

WindowsSource

pub struct Windows<T>

s.windows(size): each window is a view of s, no copy.

Trait implementations 3

ChunksSource

pub struct Chunks<T>

s.chunks(size): each chunk is a view of s, no copy.

Trait implementations 3

SliceMutSource

pub struct SliceMut<T>

A writable view of a list's elements, like Rust's &mut [T]: s[i] = x writes the list. A List<T> passed where a SliceMut<T> is expected views the whole list, and xs[a:b] there views xs[a:b]. Element i is buf[start + i].

A view lives as long as nothing resizes its list. Resizing it (push, insert, clear, ...) while a view is still used later is a compile error where the compiler can see both; elsewhere the view's methods (and each step of a walk over it) notice the list's new version and panic. Element reads and writes (s[i], get) check nothing, so a loop over a view runs as fast as one over a list: on a stale view they reach the storage the view was made of, which no other thread can reach (freezing a list that had views copies it).

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

    Source

    The number of elements.

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

    Source

    true when there are no elements.

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

    Source

    Element i, 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 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 reverse(self)§

    Source

    The elements in reverse order, in place.

  • pub const fn copy_from_slice(self, src: Slice<T>)§

    Source

    Overwrite the elements with those of src, which must be as long (s.copy_from_slice(ys[:])); panics otherwise.

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

    Source

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

  • pub const fn chunks_mut(self, size: usize) -> 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.

  • pub const fn as_slice(self) -> Slice<T>§

    Source

    The same elements as a read-only Slice.

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

    Source

    The elements in order.

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

    Source

    A new list holding the viewed elements.

  • 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 merge sort by cmp: O(n log n), n extra slots.

  • pub const unsafe fn raw_parts(self) -> (GcBuf<T>, usize, usize)§

    Source

    (buffer, start, len) of the viewed elements, for runtime calls that fill a buffer region.

  • pub const fn sort(self)§

    Source

    Ascending, stable.

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

    Source

    true when an element equals x.

Trait implementations 8

ChunksMutSource

pub struct ChunksMut<T>

xs.chunks_mut(size): each chunk is a writable view of its own part of the list, no copy.

Trait implementations 3

SliceMutIterSource

pub struct SliceMutIter<T>

s.iter() / for x in s: checks the view on every step.

Trait implementations 4

Implementations

impl List

  • pub const fn find<B: AsBytes>(self, needle: B) -> usize | none§

    Source

    self[:].find(needle).

Trait implementations 2