A dynamically-sized view into a contiguous sequence, [T]
.
Slices are a view into a block of memory represented as a pointer and a length.
// slicing a Vec let vec = vec![1, 2, 3]; let int_slice = &vec[..]; // coercing an array to a slice let str_slice: &[&str] = &["one", "two", "three"];
Slices are either mutable or shared. The shared slice type is &[T]
, while the mutable slice type is &mut [T]
, where T
represents the element type. For example, you can mutate the block of memory that a mutable slice points to:
let x = &mut [1, 2, 3]; x[1] = 7; assert_eq!(x, &[1, 7, 3]);
Here are some of the things this module contains:
There are several structs that are useful for slices, such as Iter
, which represents iteration over a slice.
There are several implementations of common traits for slices. Some examples include:
Clone
Eq
, Ord
- for slices whose element type are Eq
or Ord
.Hash
- for slices whose element type is Hash
.The slices implement IntoIterator
. The iterator yields references to the slice elements.
let numbers = &[0, 1, 2]; for n in numbers { println!("{} is a number!", n); }
The mutable slice yields mutable references to the elements:
let mut scores = [7, 8, 9]; for score in &mut scores[..] { *score += 1; }
This iterator yields mutable references to the slice's elements, so while the element type of the slice is i32
, the element type of the iterator is &mut i32
.
.iter
and .iter_mut
are the explicit methods to return the default iterators..split
, .splitn
, .chunks
, .windows
and more.See also the slice primitive type.
Chunks | An iterator over a slice in (non-overlapping) chunks ( |
ChunksMut | An iterator over a slice in (non-overlapping) mutable chunks ( |
Iter | Immutable slice iterator |
IterMut | Mutable slice iterator. |
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 |
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 |
RSplit | [ Experimental ] An iterator over subslices separated by elements that match a predicate function, starting from the end of the slice. |
RSplitMut | [ Experimental ] An iterator over the subslices of the vector which are separated by elements that match |
SliceConcatExt | [ Experimental ] An extension trait for concatenating slices |
SliceIndex | [ Experimental ] A helper trait used for indexing operations. |
from_raw_parts⚠ | Forms a slice from a pointer and a length. |
from_raw_parts_mut⚠ | Performs the same functionality as |
from_ref | [ Experimental ] Converts a reference to T into a slice of length 1 (without copying). |
from_ref_mut | [ Experimental ] Converts a reference to T into a slice of length 1 (without copying). |
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/slice/index.html