-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Iterator::array_windows()
#92394
Conversation
r? @dtolnay (rust-highfive has picked a reviewer for you, use r? to override) |
/// ``` | ||
/// #![feature(iter_array_windows)] | ||
/// | ||
/// let mut iter = "rust".chars().array_windows(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some way to assert it in compile time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
initertools
asItertools::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