summaryrefslogtreecommitdiff
path: root/rustlings/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises')
-rw-r--r--rustlings/exercises/19_smart_pointers/arc1.rs4
-rw-r--r--rustlings/exercises/19_smart_pointers/box1.rs6
-rw-r--r--rustlings/exercises/19_smart_pointers/cow1.rs9
-rw-r--r--rustlings/exercises/19_smart_pointers/rc1.rs9
4 files changed, 14 insertions, 14 deletions
diff --git a/rustlings/exercises/19_smart_pointers/arc1.rs b/rustlings/exercises/19_smart_pointers/arc1.rs
index 6bb860f..f44e4e1 100644
--- a/rustlings/exercises/19_smart_pointers/arc1.rs
+++ b/rustlings/exercises/19_smart_pointers/arc1.rs
@@ -23,13 +23,13 @@ fn main() {
let numbers: Vec<_> = (0..100u32).collect();
// TODO: Define `shared_numbers` by using `Arc`.
- // let shared_numbers = ???;
+ let shared_numbers = Arc::new(numbers);
let mut join_handles = Vec::new();
for offset in 0..8 {
// TODO: Define `child_numbers` using `shared_numbers`.
- // let child_numbers = ???;
+ let child_numbers = shared_numbers.clone();
let handle = thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
diff --git a/rustlings/exercises/19_smart_pointers/box1.rs b/rustlings/exercises/19_smart_pointers/box1.rs
index d70e1c3..ee379ca 100644
--- a/rustlings/exercises/19_smart_pointers/box1.rs
+++ b/rustlings/exercises/19_smart_pointers/box1.rs
@@ -12,18 +12,18 @@
// TODO: Use a `Box` in the enum definition to make the code compile.
#[derive(PartialEq, Debug)]
enum List {
- Cons(i32, List),
+ Cons(i32, Box<List>),
Nil,
}
// TODO: Create an empty cons list.
fn create_empty_list() -> List {
- todo!()
+ List::Nil
}
// TODO: Create a non-empty cons list.
fn create_non_empty_list() -> List {
- todo!()
+ List::Cons(1, Box::new(List::Nil))
}
fn main() {
diff --git a/rustlings/exercises/19_smart_pointers/cow1.rs b/rustlings/exercises/19_smart_pointers/cow1.rs
index 1566500..56a25b9 100644
--- a/rustlings/exercises/19_smart_pointers/cow1.rs
+++ b/rustlings/exercises/19_smart_pointers/cow1.rs
@@ -38,8 +38,7 @@ mod tests {
let vec = vec![0, 1, 2];
let mut input = Cow::from(&vec);
abs_all(&mut input);
- // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
- assert!(matches!(input, todo!()));
+ assert!(matches!(input, Cow::Borrowed(_)));
}
#[test]
@@ -51,8 +50,7 @@ mod tests {
let vec = vec![0, 1, 2];
let mut input = Cow::from(vec);
abs_all(&mut input);
- // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
- assert!(matches!(input, todo!()));
+ assert!(matches!(input, Cow::Owned(_)));
}
#[test]
@@ -63,7 +61,6 @@ mod tests {
let vec = vec![-1, 0, 1];
let mut input = Cow::from(vec);
abs_all(&mut input);
- // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
- assert!(matches!(input, todo!()));
+ assert!(matches!(input, Cow::Owned(_)));
}
}
diff --git a/rustlings/exercises/19_smart_pointers/rc1.rs b/rustlings/exercises/19_smart_pointers/rc1.rs
index ecd3438..5c9ce84 100644
--- a/rustlings/exercises/19_smart_pointers/rc1.rs
+++ b/rustlings/exercises/19_smart_pointers/rc1.rs
@@ -60,17 +60,17 @@ mod tests {
jupiter.details();
// TODO
- let saturn = Planet::Saturn(Rc::new(Sun));
+ let saturn = Planet::Saturn(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
saturn.details();
// TODO
- let uranus = Planet::Uranus(Rc::new(Sun));
+ let uranus = Planet::Uranus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
uranus.details();
// TODO
- let neptune = Planet::Neptune(Rc::new(Sun));
+ let neptune = Planet::Neptune(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
neptune.details();
@@ -92,12 +92,15 @@ mod tests {
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
// TODO
+ drop(earth);
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
// TODO
+ drop(venus);
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
// TODO
+ drop(mercury);
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
assert_eq!(Rc::strong_count(&sun), 1);