mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 13:32:23 -05:00
iterators1 solution
This commit is contained in:
parent
746cf6863d
commit
cf9041c0e4
3 changed files with 36 additions and 27 deletions
|
@ -1,8 +1,6 @@
|
||||||
// When performing operations on elements within a collection, iterators are
|
// When performing operations on elements within a collection, iterators are
|
||||||
// essential. This module helps you get familiar with the structure of using an
|
// essential. This module helps you get familiar with the structure of using an
|
||||||
// iterator and how to go through elements within an iterable collection.
|
// iterator and how to go through elements within an iterable collection.
|
||||||
//
|
|
||||||
// Make me compile by filling in the `???`s
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
|
@ -10,19 +8,18 @@ fn main() {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn iterators() {
|
fn iterators() {
|
||||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
// TODO: Create an iterator over the array.
|
||||||
|
let mut fav_fruits_iterator = ???;
|
||||||
|
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
assert_eq!(fav_fruits_iterator.next(), ???); // TODO: Replace `???`
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
assert_eq!(fav_fruits_iterator.next(), ???); // TODO: Replace `???`
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
assert_eq!(fav_fruits_iterator.next(), ???); // TODO: Replace `???`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -886,22 +886,9 @@ https://doc.rust-lang.org/stable/book/ch11-01-writing-tests.html#checking-for-pa
|
||||||
name = "iterators1"
|
name = "iterators1"
|
||||||
dir = "18_iterators"
|
dir = "18_iterators"
|
||||||
hint = """
|
hint = """
|
||||||
Step 1:
|
|
||||||
|
|
||||||
We need to apply something to the collection `my_fav_fruits` before we start to
|
|
||||||
go through it. What could that be? Take a look at the struct definition for a
|
|
||||||
vector for inspiration:
|
|
||||||
https://doc.rust-lang.org/std/vec/struct.Vec.html
|
|
||||||
|
|
||||||
Step 2 & step 3:
|
|
||||||
|
|
||||||
Very similar to the lines above and below. You've got this!
|
|
||||||
|
|
||||||
Step 4:
|
|
||||||
|
|
||||||
An iterator goes through all elements in a collection, but what if we've run
|
An iterator goes through all elements in a collection, but what if we've run
|
||||||
out of elements? What should we expect here? If you're stuck, take a look at
|
out of elements? What should we expect here? If you're stuck, take a look at
|
||||||
https://doc.rust-lang.org/std/iter/trait.Iterator.html for some ideas."""
|
https://doc.rust-lang.org/std/iter/trait.Iterator.html"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "iterators2"
|
name = "iterators2"
|
||||||
|
|
|
@ -1 +1,26 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
// 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"];
|
||||||
|
|
||||||
|
// Create an iterator over the array.
|
||||||
|
let mut fav_fruits_iterator = my_fav_fruits.iter();
|
||||||
|
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"peach"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), None);
|
||||||
|
// ^^^^ reached the end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue