Module std::slice1.0.0[][src]

連続する列への動的な大きさを持つビュー[T]

スライスはメモリの塊へのポインタと長さで表現されるビューです。

// ベクタのスライス
let vec = vec![1, 2, 3];
let int_slice = &vec[..];
// 配列のスライスへの矯正
let str_slice: &[&str] = &["one", "two", "three"];Run

スライスはミュータブルまたは共有状態のいずれかです。共有されるスライス型は&[T]ですが、ミュータブルなスライス型は&mut [T]です。ここでTは要素の型を表します。例えば、ミュータブルなスライスが指すメモリの塊を変化させることができます:

let x = &mut [1, 2, 3];
x[1] = 7;
assert_eq!(x, &[1, 7, 3]);Run

構造体

スライス上の繰り返しを表現するIterのように、スライスを扱うのに便利な構造体がいくつかあります。

トレイトの実装

いくつかのスライス共通のトレイト実装があります。例えば:

繰り返し

スライスはIntoIteratorを実装します。このイテレータはスライスの要素への参照を与えます。

let numbers = &[0, 1, 2];
for n in numbers {
    println!("{} is a number!", n);
}Run

ミュータブルなスライスは要素へのミュータブルな参照を与えます。

let mut scores = [7, 8, 9];
for score in &mut scores[..] {
    *score += 1;
}Run

このイテレータはスライスの要素へのミュータブルな参照を与えます。それゆえ、スライスの要素の型はi32ですが、イテレータの要素の型は&mut i32です。

Structs

Chunks

An iterator over a slice in (non-overlapping) chunks (chunk_size elements at a time).

ChunksMut

An iterator over a slice in (non-overlapping) mutable chunks (chunk_size elements at a time). When the slice len is not evenly divided by the chunk size, the last slice of the iteration will be the remainder.

Iter

Immutable slice iterator

IterMut

Mutable slice iterator.

RSplit

An iterator over subslices separated by elements that match a predicate function, starting from the end of the slice.

RSplitMut

An iterator over the subslices of the vector which are separated by elements that match pred, starting from the end of the slice.

RSplitN

An iterator over subslices separated by elements that match a predicate function, limited to a given number of splits, starting from the end of the slice.

RSplitNMut

An iterator over subslices separated by elements that match a predicate function, limited to a given number of splits, starting from the end of the slice.

Split

An iterator over subslices separated by elements that match a predicate function.

SplitMut

An iterator over the subslices of the vector which are separated by elements that match pred.

SplitN

An iterator over subslices separated by elements that match a predicate function, limited to a given number of splits.

SplitNMut

An iterator over subslices separated by elements that match a predicate function, limited to a given number of splits.

Windows

An iterator over overlapping subslices of length size.

ExactChunks [
Experimental
]

An iterator over a slice in (non-overlapping) chunks (chunk_size elements at a time).

ExactChunksMut [
Experimental
]

An iterator over a slice in (non-overlapping) mutable chunks (chunk_size elements at a time). When the slice len is not evenly divided by the chunk size, the last up to chunk_size-1 elements will be omitted.

Traits

SliceIndex

A helper trait used for indexing operations.

SliceConcatExt [
Experimental
]

スライスを結合するための拡張トレイト

Functions

from_mut

Converts a reference to T into a slice of length 1 (without copying).

from_raw_parts

Forms a slice from a pointer and a length.

from_raw_parts_mut

Performs the same functionality as from_raw_parts, except that a mutable slice is returned.

from_ref

Converts a reference to T into a slice of length 1 (without copying).