Adjust docs and tests for new IntoIterator impl for arrays

This commit is contained in:
Lukas Kalbertodt 2020-07-28 19:03:56 +02:00 committed by Josh Stone
parent 32aaea9c8f
commit 35b1590223
5 changed files with 8 additions and 169 deletions

View file

@ -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 { }
/// ```
///

View file

@ -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
}

View file

@ -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 <https://github.com/rust-lang/rust/pull/65819> 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`.

View file

@ -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
}

View file

@ -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 <https://github.com/rust-lang/rust/pull/65819> 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 <https://github.com/rust-lang/rust/pull/65819> 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 <https://github.com/rust-lang/rust/pull/65819> 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`.