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