Skip to content

Commit cf56162

Browse files
committed
Fix up iterator documentation with regards to for loop sugar
Fixes #23851
1 parent 80bf31d commit cf56162

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/libcore/iter.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,22 @@
2525
//! the `FromIterator` trait for creating a container from an iterator, and much
2626
//! more.
2727
//!
28-
//! ## Rust's `for` loop
28+
//! # Rust's `for` loop
2929
//!
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
3232
//! into a `loop`, for example, the `for` loop in this example is essentially
3333
//! translated to the `loop` below.
3434
//!
3535
//! ```
3636
//! let values = vec![1, 2, 3];
3737
//!
38-
//! // "Syntactical sugar" taking advantage of an iterator
39-
//! for &x in values.iter() {
38+
//! for x in values {
4039
//! println!("{}", x);
4140
//! }
4241
//!
4342
//! // Rough translation of the iteration without a `for` iterator.
44-
//! let mut it = values.iter();
43+
//! let mut it = values.into_iter();
4544
//! loop {
4645
//! match it.next() {
4746
//! Some(&x) => {
@@ -52,7 +51,8 @@
5251
//! }
5352
//! ```
5453
//!
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.
5656
5757
#![stable(feature = "rust1", since = "1.0.0")]
5858

@@ -1041,6 +1041,9 @@ pub trait FromIterator<A> {
10411041
}
10421042

10431043
/// 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.
10441047
#[stable(feature = "rust1", since = "1.0.0")]
10451048
pub trait IntoIterator {
10461049
/// The type of the elements being iterated

0 commit comments

Comments
 (0)