mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 09:48:45 +00:00
This avoids confusion between `.into_iter()` and `.iter()`. On a slice, both methods do the same (correct) thing. Using `.into_iter()` will result in a clippy warning about the slice not being consumed.
26 lines
999 B
Rust
26 lines
999 B
Rust
// When performing operations on elements within a collection, iterators are
|
|
// essential. This module helps you get familiar with the structure of using an
|
|
// iterator and how to go through elements within an iterable collection.
|
|
|
|
fn main() {
|
|
// You can optionally experiment here.
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
#[test]
|
|
fn iterators() {
|
|
let my_fav_fruits = &["banana", "custard apple", "avocado", "peach", "raspberry"];
|
|
|
|
// TODO: Create an iterator over the slice.
|
|
let mut fav_fruits_iterator = todo!();
|
|
|
|
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
|
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
|
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
|
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
|
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
|
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
|
}
|
|
}
|