collections: Update docs for slice since SliceExt was removed
A lot has changed since this doc text was last touched up, and this is just a minor edit. I remove the trait section entirely since we don't use extension traits that much anymore, so there are no significant trait hilights for this module.
This commit is contained in:
parent
00e14f1622
commit
547a48e193
1 changed files with 33 additions and 41 deletions
|
@ -13,25 +13,23 @@
|
||||||
//! The `slice` module contains useful code to help work with slice values.
|
//! The `slice` module contains useful code to help work with slice values.
|
||||||
//! Slices are a view into a block of memory represented as a pointer and a length.
|
//! Slices are a view into a block of memory represented as a pointer and a length.
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```
|
||||||
//! # #![feature(core)]
|
|
||||||
//! // slicing a Vec
|
//! // slicing a Vec
|
||||||
//! let vec = vec!(1, 2, 3);
|
//! let vec = vec![1, 2, 3];
|
||||||
//! let int_slice = vec.as_slice();
|
//! let int_slice = &vec[..];
|
||||||
//! // coercing an array to a slice
|
//! // coercing an array to a slice
|
||||||
//! let str_slice: &[&str] = &["one", "two", "three"];
|
//! let str_slice: &[&str] = &["one", "two", "three"];
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! Slices are either mutable or shared. The shared slice type is `&[T]`,
|
//! Slices are either mutable or shared. The shared slice type is `&[T]`,
|
||||||
//! while the mutable slice type is `&mut[T]`. For example, you can mutate the
|
//! while the mutable slice type is `&mut [T]`, where `T` represents the element
|
||||||
//! block of memory that a mutable slice points to:
|
//! type. For example, you can mutate the block of memory that a mutable slice
|
||||||
|
//! points to:
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```
|
||||||
//! let x: &mut[i32] = &mut [1, 2, 3];
|
//! let x = &mut [1, 2, 3];
|
||||||
//! x[1] = 7;
|
//! x[1] = 7;
|
||||||
//! assert_eq!(x[0], 1);
|
//! assert_eq!(x, &[1, 7, 3]);
|
||||||
//! assert_eq!(x[1], 7);
|
|
||||||
//! assert_eq!(x[2], 3);
|
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! Here are some of the things this module contains:
|
//! Here are some of the things this module contains:
|
||||||
|
@ -41,49 +39,43 @@
|
||||||
//! There are several structs that are useful for slices, such as `Iter`, which
|
//! There are several structs that are useful for slices, such as `Iter`, which
|
||||||
//! represents iteration over a slice.
|
//! represents iteration over a slice.
|
||||||
//!
|
//!
|
||||||
//! ## Traits
|
//! ## Trait Implementations
|
||||||
//!
|
|
||||||
//! A number of traits add methods that allow you to accomplish tasks
|
|
||||||
//! with slices, the most important being `SliceExt`. Other traits
|
|
||||||
//! apply only to slices of elements satisfying certain bounds (like
|
|
||||||
//! `Ord`).
|
|
||||||
//!
|
|
||||||
//! An example is the `slice` method which enables slicing syntax `[a..b]` that
|
|
||||||
//! returns an immutable "view" into a `Vec` or another slice from the index
|
|
||||||
//! interval `[a, b)`:
|
|
||||||
//!
|
|
||||||
//! ```rust
|
|
||||||
//! fn main() {
|
|
||||||
//! let numbers = [0, 1, 2];
|
|
||||||
//! let last_numbers = &numbers[1..3];
|
|
||||||
//! // last_numbers is now &[1, 2]
|
|
||||||
//! }
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! ## Implementations of other traits
|
|
||||||
//!
|
//!
|
||||||
//! There are several implementations of common traits for slices. Some examples
|
//! There are several implementations of common traits for slices. Some examples
|
||||||
//! include:
|
//! include:
|
||||||
//!
|
//!
|
||||||
//! * `Clone`
|
//! * `Clone`
|
||||||
//! * `Eq`, `Ord` - for immutable slices whose element type are `Eq` or `Ord`.
|
//! * `Eq`, `Ord` - for slices whose element type are `Eq` or `Ord`.
|
||||||
//! * `Hash` - for slices whose element type is `Hash`
|
//! * `Hash` - for slices whose element type is `Hash`
|
||||||
//!
|
//!
|
||||||
//! ## Iteration
|
//! ## Iteration
|
||||||
//!
|
//!
|
||||||
//! The method `iter()` returns an iteration value for a slice. The iterator
|
//! The slices implement `IntoIterator`. The iterators of yield references
|
||||||
//! yields references to the slice's elements, so if the element
|
//! to the slice elements.
|
||||||
//! type of the slice is `isize`, the element type of the iterator is `&isize`.
|
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```
|
||||||
//! let numbers = [0, 1, 2];
|
//! let numbers = &[0, 1, 2];
|
||||||
//! for &x in numbers.iter() {
|
//! for n in numbers {
|
||||||
//! println!("{} is a number!", x);
|
//! println!("{} is a number!", n);
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! * `.iter_mut()` returns an iterator that allows modifying each value.
|
//! The mutable slice yields mutable references to the elements:
|
||||||
//! * Further iterators exist that split, chunk or permute the slice.
|
//!
|
||||||
|
//! ```
|
||||||
|
//! 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.
|
||||||
|
//! * Further methods that return iterators are `.split()`, `.splitn()`,
|
||||||
|
//! `.chunks()`, `.windows()` and more.
|
||||||
|
|
||||||
#![doc(primitive = "slice")]
|
#![doc(primitive = "slice")]
|
||||||
#![stable(feature = "rust1", since = "1.0.0")]
|
#![stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
Loading…
Reference in a new issue