Skip to content

Commit 6778159

Browse files
JulienBerkaneDana Binkley
authored andcommitted
Clean commit
1 parent 846af1f commit 6778159

5 files changed

Lines changed: 233 additions & 8 deletions

File tree

courses/rust_essentials/170_iterators/88-iterators.lab.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
Lab
33
=====
44

5-
-----
6-
TBD
7-
-----
5+
------------------
6+
Lab Instructions
7+
------------------
88

9-
TBD
9+
- Solve for compilation errors
10+
11+
- Follow the hints!
12+
13+
- Success is
14+
15+
- Code that compiles
16+
17+
- ...and that follows any behavior indicated within the hints!
18+

courses/rust_essentials/170_iterators/lab/answer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lab"
2+
name = "answer"
33
version = "1.0.0"
44
edition = "2024"
55

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,113 @@
1+
//! Lab (answer)
2+
//! Iterators
3+
//!
4+
//! Fix all the compile errors below and/or follow the hints provided
5+
//!
6+
17
fn main() {
2-
println!("TBD");
8+
// TASK 1 - Looping via Array Indices
9+
// Hint: Print ALL values. Check the output to verify you printed all elements
10+
let array = [2, 4, 6, 8];
11+
for idx in 0..4 {
12+
println!("{}", array[idx]);
13+
}
14+
15+
// TASK 2 - Using Our Iterator
16+
// Hint: The 'for' loop calls '.next()' under the hood
17+
struct SliceIter<'s> {
18+
slice: &'s [i32],
19+
idx: usize,
20+
}
21+
impl<'s> Iterator for SliceIter<'s> {
22+
type Item = &'s i32;
23+
fn next(&mut self) -> Option<Self::Item> {
24+
if self.idx == self.slice.len() {
25+
None
26+
} else {
27+
let next = &self.slice[self.idx];
28+
self.idx += 1;
29+
Some(next)
30+
}
31+
}
32+
}
33+
let numbers = [10, 20, 30];
34+
let iter = SliceIter {
35+
slice: &numbers,
36+
idx: 0,
37+
};
38+
for my_iter in iter {
39+
println!("The number is: {}", my_iter);
40+
}
41+
42+
// TASK 3 - Getting an Iterator - 'iter()'
43+
// Hint: Collections themselves are not iterators
44+
// Using 'iter()' does not consume the collection
45+
let numbers_2 = vec![10, 20, 30];
46+
for n in numbers_2.iter() {
47+
println!("{n}");
48+
}
49+
println!("Done printing {} items.", numbers_2.len());
50+
51+
// TASK 4 - Common Iterator Adapters
52+
// Hint: 'map' transforms values during iteration, 'filter' selects values matching condition
53+
fn double(x: &i32) -> i32 {
54+
x * 2
55+
}
56+
fn is_even(x: &&i32) -> bool {
57+
**x % 2 == 0
58+
}
59+
let flush = vec![10, 11, 12, 13, 14];
60+
61+
// Multiply all values by 2
62+
for elem in flush.iter().map(double) {
63+
println!("Value multiplied by 2 is: {elem}");
64+
}
65+
// Print only values divisible by 2
66+
for elem in flush.iter().filter(is_even) {
67+
println!("This value is divisible by 2: {elem}");
68+
}
69+
70+
// TASK 5 - Common Consumers
71+
// Hint: 'sum' adds all values and return a single value, 'any' returns True if any value matches condition
72+
fn is_freezing(temp: &i32) -> bool {
73+
*temp < 0
74+
}
75+
let hand = [1, 2, 3, 4, 5];
76+
// Sum all values into 'Total'
77+
let total: i32 = hand.iter().sum();
78+
println!("The total is: {total}");
79+
80+
let temperatures = [22, 28, -2, 15, 30];
81+
// Print the warning if any temperature is below 0
82+
if temperatures.iter().any(is_freezing){
83+
println!("Warning: Freezing temperatures detected!");
84+
} else {
85+
println!("All temperatures are above freezing.");
86+
}
87+
88+
// TASK 6 - Chaining
89+
// Hint: Chaining allows you to create a new set of data before consuming
90+
fn is_even_owned(x: &i32) -> bool {
91+
*x % 2 == 0
92+
}
93+
fn square (x: i32) -> i32 {
94+
x * x
95+
}
96+
let result: i32 = (1..=10) // Range: 1, 2, 3, ..., 10
97+
.filter(is_even_owned) // Only select even values
98+
.map(square) // Square values
99+
.sum(); // Total: 220
100+
println!("Sum of even squares: {}", result);
101+
102+
// TASK 7 - Collecting "Result"
103+
// Hint: 'collect()' stores results in a collection
104+
fn parse_i32(s: &str) -> Result<i32, std::num::ParseIntError> {
105+
s.parse::<i32>()
106+
}
107+
let good_strings = vec!["1", "2", "42"];
108+
let good_numbers: Result<Vec<i32>, _> = good_strings
109+
.into_iter()
110+
.map(parse_i32)
111+
.collect();
112+
println!("good_numbers: {:?}", good_numbers);
3113
}

courses/rust_essentials/170_iterators/lab/prompt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lab"
2+
name = "prompt"
33
version = "1.0.0"
44
edition = "2024"
55

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,109 @@
1+
//! Lab (prompt)
2+
//! Iterators
3+
//!
4+
//! Fix all the compile errors below and/or follow the hints provided
5+
//!
6+
17
fn main() {
2-
println!("TBD");
8+
// TASK 1 - Looping via Array Indices
9+
// Hint: Print ALL values. Check the output to verify you printed all elements
10+
let array = [2, 4, 6, 8];
11+
for idx in 1..4 {
12+
println!("{}", X);
13+
}
14+
15+
// TASK 2 - Using Our Iterator
16+
// Hint: The 'for' loop calls .next() under the hood
17+
struct SliceIter<'s> {
18+
slice: &'s [i32],
19+
idx: usize,
20+
}
21+
impl<'s> Iterator for SliceIter<'s> {
22+
type Item = &'s i32;
23+
fn next(&mut self) -> Option<Self::Item> {
24+
if self.idx == self.slice.len() {
25+
None
26+
} else {
27+
let next = &self.slice[self.idx];
28+
self.idx += 1;
29+
Some(next)
30+
}
31+
}
32+
}
33+
let numbers = [10, 20, 30];
34+
let iter = SliceIter {
35+
slice: &numbers,
36+
idx: 0,
37+
};
38+
for X in Y {
39+
println!("The number is: {}", X);
40+
}
41+
42+
// TASK 3 - Getting an Iterator - 'iter()'
43+
// Hint: Collections themselves are not iterators
44+
let numbers_2 = vec![10, 20, 30];
45+
for n in numbers_2 {
46+
println!("{n}");
47+
}
48+
println!("Done printing {} items.", numbers_2.len());
49+
50+
// TASK 4 - Common Iterator Adapters
51+
// Hint: 'map' transforms values during iteration, 'filter' selects values matching condition
52+
fn double(x: &i32) -> i32 {
53+
x * 2
54+
}
55+
fn is_even(x: &&i32) -> bool {
56+
**x % 2 == 0
57+
}
58+
let flush = vec![10, 11, 12, 13, 14];
59+
// Multiply all values by 2
60+
for elem in flush.iter {
61+
println!("Value multiplied by 2 is: {elem}");
62+
}
63+
// Print only values divisible by 2
64+
for elem in flush.iter {
65+
println!("this value is divisible by 2: {elem}");
66+
}
67+
68+
// TASK 5 - Common consumers
69+
// Hint: 'sum' adds all values and return a single value, 'any' returns True if any value matches condition
70+
fn is_freezing(temp: &i32) -> bool {
71+
*temp < 0
72+
}
73+
let hand = [1, 2, 3, 4, 5];
74+
// Sum all values into 'Total'
75+
let total: i32 = hand.iter;
76+
println!("The total is: {total}");
77+
let temperatures = [22, 28, -2, 15, 30];
78+
// Print the warning if any temperature is below 0
79+
if temperatures.iter{
80+
println!("Warning: Freezing temperatures detected!");
81+
} else {
82+
println!("All temperatures are above freezing.");
83+
}
84+
85+
// TASK 6 - Chaining
86+
// Hint: Chaining allows you to create a new set of data before consuming
87+
fn is_even_owned(x: &i32) -> bool {
88+
*x % 2 == 0
89+
}
90+
fn square (x: i32) -> i32 {
91+
x * x
92+
}
93+
let result: i32 = (1..=10) // Range: 1, 2, 3, ..., 10
94+
.X // Only select even values
95+
.Y // Square values
96+
.sum(); // Total: 220
97+
println!("Sum of even squares: {}", result);
98+
99+
// TASK 7 - Collecting "Result"
100+
// Hint: 'collect()' stores results in a collection
101+
fn parse_i32(s: &str) -> Result<i32, std::num::ParseIntError> {
102+
s.parse::<i32>()
103+
}
104+
let good_strings = vec!["1", "2", "42"];
105+
let good_numbers: Result<Vec<i32>, _> = good_strings
106+
.into_iter()
107+
.map(parse_i32)
108+
println!("good_numbers: {:?}", good_numbers);
3109
}

0 commit comments

Comments
 (0)