Skip to content

Commit 547a48e

Browse files
author
Ulrik Sverdrup
committed
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.
1 parent 00e14f1 commit 547a48e

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

src/libcollections/slice.rs

+33-41
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@
1313
//! The `slice` module contains useful code to help work with slice values.
1414
//! Slices are a view into a block of memory represented as a pointer and a length.
1515
//!
16-
//! ```rust
17-
//! # #![feature(core)]
16+
//! ```
1817
//! // slicing a Vec
19-
//! let vec = vec!(1, 2, 3);
20-
//! let int_slice = vec.as_slice();
18+
//! let vec = vec![1, 2, 3];
19+
//! let int_slice = &vec[..];
2120
//! // coercing an array to a slice
2221
//! let str_slice: &[&str] = &["one", "two", "three"];
2322
//! ```
2423
//!
2524
//! Slices are either mutable or shared. The shared slice type is `&[T]`,
26-
//! while the mutable slice type is `&mut[T]`. For example, you can mutate the
27-
//! block of memory that a mutable slice points to:
25+
//! while the mutable slice type is `&mut [T]`, where `T` represents the element
26+
//! type. For example, you can mutate the block of memory that a mutable slice
27+
//! points to:
2828
//!
29-
//! ```rust
30-
//! let x: &mut[i32] = &mut [1, 2, 3];
29+
//! ```
30+
//! let x = &mut [1, 2, 3];
3131
//! x[1] = 7;
32-
//! assert_eq!(x[0], 1);
33-
//! assert_eq!(x[1], 7);
34-
//! assert_eq!(x[2], 3);
32+
//! assert_eq!(x, &[1, 7, 3]);
3533
//! ```
3634
//!
3735
//! Here are some of the things this module contains:
@@ -41,49 +39,43 @@
4139
//! There are several structs that are useful for slices, such as `Iter`, which
4240
//! represents iteration over a slice.
4341
//!
44-
//! ## Traits
45-
//!
46-
//! A number of traits add methods that allow you to accomplish tasks
47-
//! with slices, the most important being `SliceExt`. Other traits
48-
//! apply only to slices of elements satisfying certain bounds (like
49-
//! `Ord`).
50-
//!
51-
//! An example is the `slice` method which enables slicing syntax `[a..b]` that
52-
//! returns an immutable "view" into a `Vec` or another slice from the index
53-
//! interval `[a, b)`:
54-
//!
55-
//! ```rust
56-
//! fn main() {
57-
//! let numbers = [0, 1, 2];
58-
//! let last_numbers = &numbers[1..3];
59-
//! // last_numbers is now &[1, 2]
60-
//! }
61-
//! ```
62-
//!
63-
//! ## Implementations of other traits
42+
//! ## Trait Implementations
6443
//!
6544
//! There are several implementations of common traits for slices. Some examples
6645
//! include:
6746
//!
6847
//! * `Clone`
69-
//! * `Eq`, `Ord` - for immutable slices whose element type are `Eq` or `Ord`.
48+
//! * `Eq`, `Ord` - for slices whose element type are `Eq` or `Ord`.
7049
//! * `Hash` - for slices whose element type is `Hash`
7150
//!
7251
//! ## Iteration
7352
//!
74-
//! The method `iter()` returns an iteration value for a slice. The iterator
75-
//! yields references to the slice's elements, so if the element
76-
//! type of the slice is `isize`, the element type of the iterator is `&isize`.
53+
//! The slices implement `IntoIterator`. The iterators of yield references
54+
//! to the slice elements.
7755
//!
78-
//! ```rust
79-
//! let numbers = [0, 1, 2];
80-
//! for &x in numbers.iter() {
81-
//! println!("{} is a number!", x);
56+
//! ```
57+
//! let numbers = &[0, 1, 2];
58+
//! for n in numbers {
59+
//! println!("{} is a number!", n);
8260
//! }
8361
//! ```
8462
//!
85-
//! * `.iter_mut()` returns an iterator that allows modifying each value.
86-
//! * Further iterators exist that split, chunk or permute the slice.
63+
//! The mutable slice yields mutable references to the elements:
64+
//!
65+
//! ```
66+
//! let mut scores = [7, 8, 9];
67+
//! for score in &mut scores[..] {
68+
//! *score += 1;
69+
//! }
70+
//! ```
71+
//!
72+
//! This iterator yields mutable references to the slice's elements, so while the element
73+
//! type of the slice is `i32`, the element type of the iterator is `&mut i32`.
74+
//!
75+
//! * `.iter()` and `.iter_mut()` are the explicit methods to return the default
76+
//! iterators.
77+
//! * Further methods that return iterators are `.split()`, `.splitn()`,
78+
//! `.chunks()`, `.windows()` and more.
8779
8880
#![doc(primitive = "slice")]
8981
#![stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)