Skip to content

Commit 34aafa8

Browse files
Main exercises
1 parent 278a1f1 commit 34aafa8

File tree

6 files changed

+52
-23
lines changed

6 files changed

+52
-23
lines changed

exercises/error_handling/errors5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// This exercise uses some concepts that we won't get to until later in the course, like `Box` and the
66
// `From` trait. It's not important to understand them in detail right now, but you can read ahead if you like.
7-
// For now, think of the `Box<dyn ...>` type as an "I want anything that does ???" type, which, given
7+
// For now, think of the `Box<dyn ???>` type as an "I want anything that does ???" type, which, given
88
// Rust's usual standards for runtime safety, should strike you as somewhat lenient!
99

1010
// In short, this particular use case for boxes is for when you want to own a value and you care only that it is a

exercises/iterators/iterators1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// I AM NOT DONE
1212

13-
fn main () {
13+
fn main() {
1414
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
1515

1616
let mut my_iterable_fav_fruits = ???; // TODO: Step 1

exercises/macros/macros4.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// I AM NOT DONE
55

6+
#[rustfmt::skip]
67
macro_rules! my_macro {
78
() => {
89
println!("Check out my macro!");

exercises/smart_pointers/arc1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main() {
3232
for offset in 0..8 {
3333
let child_numbers = // TODO
3434
joinhandles.push(thread::spawn(move || {
35-
let sum: u32 = child_numbers.iter().filter(|n| *n % 8 == offset).sum();
35+
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
3636
println!("Sum of offset {} is {}", offset, sum);
3737
}));
3838
}

exercises/smart_pointers/cow1.rs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// Cow is a clone-on-write smart pointer.
55
// It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required.
66
// The type is designed to work with general borrowed data via the Borrow trait.
7+
//
8+
// This exercise is meant to show you what to expect when passing data to Cow.
9+
// Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the TODO markers.
710

811
// I AM NOT DONE
912

@@ -20,29 +23,52 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
2023
input
2124
}
2225

23-
fn main() {
24-
// No clone occurs because `input` doesn't need to be mutated.
25-
let slice = [0, 1, 2];
26-
let mut input = Cow::from(&slice[..]);
27-
match abs_all(&mut input) {
28-
Cow::Borrowed(_) => println!("I borrowed the slice!"),
29-
_ => panic!("expected borrowed value"),
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[test]
31+
fn reference_mutation() -> Result<(), &'static str> {
32+
// Clone occurs because `input` needs to be mutated.
33+
let slice = [-1, 0, 1];
34+
let mut input = Cow::from(&slice[..]);
35+
match abs_all(&mut input) {
36+
Cow::Owned(_) => Ok(()),
37+
_ => Err("Expected owned value"),
38+
}
3039
}
3140

32-
// Clone occurs because `input` needs to be mutated.
33-
let slice = [-1, 0, 1];
34-
let mut input = Cow::from(&slice[..]);
35-
match abs_all(&mut input) {
36-
Cow::Owned(_) => println!("I modified the slice and now own it!"),
37-
_ => panic!("expected owned value"),
41+
#[test]
42+
fn reference_no_mutation() -> Result<(), &'static str> {
43+
// No clone occurs because `input` doesn't need to be mutated.
44+
let slice = [0, 1, 2];
45+
let mut input = Cow::from(&slice[..]);
46+
match abs_all(&mut input) {
47+
// TODO
48+
}
3849
}
3950

40-
// No clone occurs because `input` is already owned.
41-
let slice = vec![-1, 0, 1];
42-
let mut input = Cow::from(slice);
43-
match abs_all(&mut input) {
44-
// TODO
45-
Cow::Borrowed(_) => println!("I own this slice!"),
46-
_ => panic!("expected borrowed value"),
51+
#[test]
52+
fn owned_no_mutation() -> Result<(), &'static str> {
53+
// We can also pass `slice` without `&` so Cow owns it directly.
54+
// In this case no mutation occurs and thus also no clone,
55+
// but the result is still owned because it always was.
56+
let slice = vec![0, 1, 2];
57+
let mut input = Cow::from(slice);
58+
match abs_all(&mut input) {
59+
// TODO
60+
}
61+
}
62+
63+
#[test]
64+
fn owned_mutation() -> Result<(), &'static str> {
65+
// Of course this is also the case if a mutation does occur.
66+
// In this case the call to `to_mut()` returns a reference to
67+
// the same data as before.
68+
let slice = vec![-1, 0, 1];
69+
let mut input = Cow::from(slice);
70+
match abs_all(&mut input) {
71+
// TODO
72+
}
4773
}
4874
}

exercises/vecs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ the other useful data structure, hash maps, later.
1313
## Further information
1414

1515
- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)
16+
- [`iter_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.iter_mut)
17+
- [`map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map)

0 commit comments

Comments
 (0)