|
26 | 26 | //! in reverse, the `FromIterator` trait for creating a container from an
|
27 | 27 | //! iterator, and much more.
|
28 | 28 | //!
|
29 |
| -//! ## Rust's `for` loop |
| 29 | +//! # Rust's `for` loop |
30 | 30 | //!
|
31 |
| -//! The special syntax used by rust's `for` loop is based around the `Iterator` |
32 |
| -//! trait defined in this module. For loops can be viewed as a syntactical |
33 |
| -//! expansion into a `loop`, for example, the `for` loop in this example is |
34 |
| -//! essentially translated to the `loop` below. |
| 31 | +//! The special syntax used by rust's `for` loop is based around the |
| 32 | +//! `IntoIterator` trait defined in this module. `for` loops can be viewed as a |
| 33 | +//! syntactical expansion into a `loop`, for example, the `for` loop in this |
| 34 | +//! example is essentially translated to the `loop` below. |
35 | 35 | //!
|
36 | 36 | //! ```
|
37 | 37 | //! let values = vec![1, 2, 3];
|
38 | 38 | //!
|
39 |
| -//! // "Syntactical sugar" taking advantage of an iterator |
40 |
| -//! for &x in values.iter() { |
| 39 | +//! for x in values { |
41 | 40 | //! println!("{}", x);
|
42 | 41 | //! }
|
43 | 42 | //!
|
44 | 43 | //! // Rough translation of the iteration without a `for` iterator.
|
45 |
| -//! let mut it = values.iter(); |
| 44 | +//! let mut it = values.into_iter(); |
46 | 45 | //! loop {
|
47 | 46 | //! match it.next() {
|
48 | 47 | //! Some(&x) => {
|
|
53 | 52 | //! }
|
54 | 53 | //! ```
|
55 | 54 | //!
|
56 |
| -//! This `for` loop syntax can be applied to any iterator over any type. |
| 55 | +//! Because `Iterator`s implement `IntoIterator`, this `for` loop syntax can be applied to any |
| 56 | +//! iterator over any type. |
57 | 57 |
|
58 | 58 | #![stable(feature = "rust1", since = "1.0.0")]
|
59 | 59 |
|
@@ -1057,6 +1057,9 @@ pub trait FromIterator<A> {
|
1057 | 1057 | }
|
1058 | 1058 |
|
1059 | 1059 | /// Conversion into an `Iterator`
|
| 1060 | +/// |
| 1061 | +/// Implementing this trait allows you to use your type with Rust's `for` loop. See |
| 1062 | +/// the [module level documentation](../index.html) for more details. |
1060 | 1063 | #[stable(feature = "rust1", since = "1.0.0")]
|
1061 | 1064 | pub trait IntoIterator {
|
1062 | 1065 | /// The type of the elements being iterated
|
|
0 commit comments