diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 64b22b64f4b..ad4737ec2f6 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -498,7 +498,7 @@ mod prim_pointer {} /// - [`Copy`] /// - [`Clone`] /// - [`Debug`] -/// - [`IntoIterator`] (implemented for `&[T; N]` and `&mut [T; N]`) +/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`) /// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`] /// - [`Hash`] /// - [`AsRef`], [`AsMut`] @@ -526,31 +526,16 @@ mod prim_pointer {} /// assert_eq!([1, 2], &array[1..]); /// /// // This loop prints: 0 1 2 -/// for x in &array { +/// for x in array { /// print!("{} ", x); /// } /// ``` /// -/// An array itself is not iterable: +/// You can also iterate over reference to the array's elements: /// -/// ```compile_fail,E0277 +/// ``` /// let array: [i32; 3] = [0; 3]; /// -/// for x in array { } -/// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied -/// ``` -/// -/// The solution is to coerce the array to a slice by calling a slice method: -/// -/// ``` -/// # let array: [i32; 3] = [0; 3]; -/// for x in array.iter() { } -/// ``` -/// -/// You can also use the array reference's [`IntoIterator`] implementation: -/// -/// ``` -/// # let array: [i32; 3] = [0; 3]; /// for x in &array { } /// ``` /// diff --git a/src/test/ui/iterators/array-of-ranges.rs b/src/test/ui/iterators/array-of-ranges.rs index d2dfc7ec327..037540a3e89 100644 --- a/src/test/ui/iterators/array-of-ranges.rs +++ b/src/test/ui/iterators/array-of-ranges.rs @@ -1,23 +1,16 @@ +// check-pass + fn main() { for _ in [0..1] {} -//~^ ERROR is not an iterator for _ in [0..=1] {} -//~^ ERROR is not an iterator for _ in [0..] {} -//~^ ERROR is not an iterator for _ in [..1] {} -//~^ ERROR is not an iterator for _ in [..=1] {} -//~^ ERROR is not an iterator let start = 0; let end = 0; for _ in [start..end] {} -//~^ ERROR is not an iterator let array_of_range = [start..end]; for _ in array_of_range {} -//~^ ERROR is not an iterator for _ in [0..1, 2..3] {} -//~^ ERROR is not an iterator for _ in [0..=1] {} -//~^ ERROR is not an iterator } diff --git a/src/test/ui/iterators/array-of-ranges.stderr b/src/test/ui/iterators/array-of-ranges.stderr deleted file mode 100644 index 7d58eb948ea..00000000000 --- a/src/test/ui/iterators/array-of-ranges.stderr +++ /dev/null @@ -1,102 +0,0 @@ -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:2:14 - | -LL | for _ in [0..1] {} - | ^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 1]` - = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:4:14 - | -LL | for _ in [0..=1] {} - | ^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[RangeInclusive<{integer}>; 1]` - = note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeFrom<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:6:14 - | -LL | for _ in [0..] {} - | ^^^^^ if you meant to iterate from a value onwards, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[RangeFrom<{integer}>; 1]` - = note: `[start..]` is an array of one `RangeFrom`; you might have meant to have a `RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an unbounded iterator will run forever unless you `break` or `return` from within the loop - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeFrom<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeTo<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:8:14 - | -LL | for _ in [..1] {} - | ^^^^^ if you meant to iterate until a value, remove the square brackets and add a starting value - | - = help: the trait `Iterator` is not implemented for `[RangeTo<{integer}>; 1]` - = note: `[..end]` is an array of one `RangeTo`; you might have meant to have a bounded `Range` without the brackets: `0..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeTo<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeToInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:10:14 - | -LL | for _ in [..=1] {} - | ^^^^^^ if you meant to iterate until a value (including it), remove the square brackets and add a starting value - | - = help: the trait `Iterator` is not implemented for `[RangeToInclusive<{integer}>; 1]` - = note: `[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a bounded `RangeInclusive` without the brackets: `0..=end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeToInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:14:14 - | -LL | for _ in [start..end] {} - | ^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 1]` - = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:17:14 - | -LL | for _ in array_of_range {} - | ^^^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 1]` - = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 2]` is not an iterator - --> $DIR/array-of-ranges.rs:19:14 - | -LL | for _ in [0..1, 2..3] {} - | ^^^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 2]` - = note: required by `into_iter` - -error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:21:14 - | -LL | for _ in [0..=1] {} - | ^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[RangeInclusive<{integer}>; 1]` - = note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error: aborting due to 9 previous errors - -For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/iterators/array.rs b/src/test/ui/iterators/array.rs index 33c84f6fa35..5985c74e11f 100644 --- a/src/test/ui/iterators/array.rs +++ b/src/test/ui/iterators/array.rs @@ -1,9 +1,8 @@ +// check-pass + fn main() { for _ in [1, 2] {} -//~^ ERROR is not an iterator let x = [1, 2]; for _ in x {} -//~^ ERROR is not an iterator for _ in [1.0, 2.0] {} -//~^ ERROR is not an iterator } diff --git a/src/test/ui/iterators/array.stderr b/src/test/ui/iterators/array.stderr deleted file mode 100644 index 7e2b600fb7a..00000000000 --- a/src/test/ui/iterators/array.stderr +++ /dev/null @@ -1,36 +0,0 @@ -error[E0277]: `[{integer}; 2]` is not an iterator - --> $DIR/array.rs:2:14 - | -LL | for _ in [1, 2] {} - | ^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{integer}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]` - = note: required by `into_iter` - -error[E0277]: `[{integer}; 2]` is not an iterator - --> $DIR/array.rs:5:14 - | -LL | for _ in x {} - | ^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{integer}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]` - = note: required by `into_iter` - -error[E0277]: `[{float}; 2]` is not an iterator - --> $DIR/array.rs:7:14 - | -LL | for _ in [1.0, 2.0] {} - | ^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{float}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{float}; 2]` - = note: required by `into_iter` - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0277`.