Add Iterator::array_windows()#92394
Conversation
|
r? @dtolnay (rust-highfive has picked a reviewer for you, use r? to override) |
There was a problem hiding this comment.
| /// let mut iter = "rust".chars().array_windows(); | |
| /// let mut iter = "rust".chars().array_windows::<2>(); |
I think you missed it?
There was a problem hiding this comment.
It's not required because of type inference
64c5c01 to
6f19676
Compare
|
This involves a lot of |
|
Thanks @the8472, you make some good points. I do think in a lot of cases it might be more performant to collect into a |
|
Perhaps reviving #82413 would be a better option since the map closure can take a reference to the internal iterator state and thus incur no cloning overhead. Additionally it amortizes the rotation cost by increasing the internal buffer size. |
| { | ||
| #[inline] | ||
| pub(in crate::iter) fn new(iter: I) -> Self { | ||
| assert!(N != 0); |
There was a problem hiding this comment.
Is there some way to assert it in compile time?
There was a problem hiding this comment.
There was a problem hiding this comment.
It would be ideal but I couldn't get it to work. I tried it for #92393 and get the following error
error[E0401]: can't use generic parameters from outer function
--> library/core/src/iter/adapters/array_chunks.rs:86:31
|
81 | impl<I, const N: usize> ArrayChunks<I, N>
| - const parameter from outer function
...
86 | const _: () = assert!(N != 0, "chunk size must be non-zero");
| ^ use of generic parameter from outer function
For more information about this error, try `rustc --explain E0401`.
And if I make the const part of the impl block it doesn't result in a compile time error.
|
Based on the discussion above I'm closing this. I might look at reviving #82413 instead. |
This has been similarly implemented as
.tuple_windows()initertoolsasItertools::tuple_windows(). But it makes more sense with arrays since all elements are the same type.I will update stability attributes with a tracking issue if accepted.
See also #92393