Skip to content

Commit bbdc5c6

Browse files
refactor(cow1): replace main with tests
Following the discussion in #1195 this is the best I could come up with. The issue for me (and apparently a few other learners) was that the code needed to complete the exercise was not _missing_, but was rather there but wrong. In the end, what made the difference between this exercise and others (for me) was that in this exercise I was supposed to learn what to *expect* of an output. So I think it makes sense here to let the learner modify the tests and not the code itself. Fixes #1195 Signed-off-by: Daan Wynen <black.puppydog@gmx.de> # Conflicts: # info.toml
1 parent 149e0c8 commit bbdc5c6

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

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
}

info.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,11 +1010,11 @@ https://doc.rust-lang.org/stable/book/ch16-00-concurrency.html
10101010
[[exercises]]
10111011
name = "cow1"
10121012
path = "exercises/smart_pointers/cow1.rs"
1013-
mode = "compile"
1013+
mode = "test"
10141014
hint = """
1015-
Since the vector is already owned, the `Cow` type doesn't need to clone it.
1015+
If Cow already owns the data it doesn't need to clone it when to_mut() is called.
10161016
1017-
Checkout https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
1017+
Check out https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
10181018
on the `Cow` type.
10191019
"""
10201020

0 commit comments

Comments
 (0)