1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-20 22:36:03 +02:00

Use .iter() instead of (&values).into_iter() (#2519)

The `(&values).into_iter()` construct used in the solution to the
iterators exercise is kind of awkward and always gets questions from
students. I think the better thing would be to use the `iter` method to
get the initial iterator, as that's the more idiomatic way to. It's also
an opportunity to point out that there are helper methods for getting an
iterator for a collection.
This commit is contained in:
Nicole L 2024-12-16 12:22:44 -08:00 committed by GitHub
parent c33a9b2ca4
commit 2e8d5d3d9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,8 +22,8 @@
/// Element `n` of the result is `values[(n+offset)%len] - values[n]`.
fn offset_differences(offset: usize, values: Vec<i32>) -> Vec<i32> {
// ANCHOR_END: offset_differences
let a = (&values).into_iter();
let b = (&values).into_iter().cycle().skip(offset);
let a = values.iter();
let b = values.iter().cycle().skip(offset);
a.zip(b).map(|(a, b)| *b - *a).collect()
}