Help to understand Iterator type (&[i32; 33] is not an iterator)


Hi all, I’m new in rust. Playing with “take_while” and “zip” i got an error.

fn main () {
    let xs: [i32; 33] = [3; 33];
    let fine: [i32; 32] = [3; 32];
    let p = xs.iter().zip(&fine)
      .take_while(|(x, y)| x == y)
    let error: [i32; 33] = [3; 33];
    let p = xs.iter().zip(&error)
      .take_while(|(x, y)| x == y)



   Compiling playground v0.0.1 (/playground)
error[E0277]: `&[i32; 33]` is not an iterator
  --> src/
11 |     let p = xs.iter().zip(&error)
   |                       ^^^ `&[i32; 33]` is not an iterator
   = help: the trait `std::iter::Iterator` is not implemented for `&[i32; 33]`
   = note: required because of the requirements on the impl of `std::iter::IntoIterator` for `&[i32; 33]`

error[E0599]: no method named `take_while` found for type `std::iter::Zip<std::slice::Iter<'_, i32>, &[i32; 33]>` in the current scope
  --> src/
12 |       .take_while(|(x, y)| x == y)
   |        ^^^^^^^^^^
   = note: the method `take_while` exists but the following trait bounds were not satisfied:
           `&mut std::iter::Zip<std::slice::Iter<'_, i32>, &[i32; 33]> : std::iter::Iterator`
           `std::iter::Zip<std::slice::Iter<'_, i32>, &[i32; 33]> : std::iter::Iterator`

error: aborting due to 2 previous errors

Some errors occurred: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Why can’t I use an array containing more than 32 elements? With “fine” variable compile without error.



The problem is in fact that, while const generics are still in development, any trait must be implemented for every array size independently.
In this case, zip method takes argument which must implement IntoIterator (and this makes sense, since it will yield values from this iterator). But IntoIterator (as well as any other trait) is implemented only on array with size up to 32 - the size must be bounded, and this seems to be the reasonable bound, since large arrays are hardly usable anyway.

1 Like


In addition to what @Cerberuser said, you can use &error[…] to get a &[i32]. That’s also the reason why you didn’t have any issue with xs.




Thanks for the answer. More info How live with Arrays are fixed size