Skip to content

Commit 7202077

Browse files
committed
solve smart_pointers
1 parent 7d35f76 commit 7202077

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

exercises/smart_pointers/arc1.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
//
2222
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
2323

24-
// I AM NOT DONE
2524

2625
#![forbid(unused_imports)] // Do not change this, (or the next) line.
2726
use std::sync::Arc;
2827
use std::thread;
2928

3029
fn main() {
3130
let numbers: Vec<_> = (0..100u32).collect();
32-
let shared_numbers = // TODO
33-
let mut joinhandles = Vec::new();
31+
let shared_numbers = Arc::new(numbers);// TODO
32+
let mut joinhandles = vec![];
3433

3534
for offset in 0..8 {
36-
let child_numbers = // TODO
35+
let child_numbers = Arc::clone(&shared_numbers);// TODO
3736
joinhandles.push(thread::spawn(move || {
3837
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
3938
println!("Sum of offset {} is {}", offset, sum);

exercises/smart_pointers/box1.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
//
1919
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
2020

21-
// I AM NOT DONE
22-
2321
#[derive(PartialEq, Debug)]
2422
pub enum List {
25-
Cons(i32, List),
23+
Cons(i32, Box<List>),
2624
Nil,
2725
}
2826

@@ -33,13 +31,14 @@ fn main() {
3331
create_non_empty_list()
3432
);
3533
}
36-
34+
use crate::List::Cons;
35+
use crate::List::Nil;
3736
pub fn create_empty_list() -> List {
38-
todo!()
37+
Nil
3938
}
4039

4140
pub fn create_non_empty_list() -> List {
42-
todo!()
41+
Cons(1, Box::new(Cons(2, Box::new(Nil))))
4342
}
4443

4544
#[cfg(test)]

exercises/smart_pointers/cow1.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//
1313
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
1414

15-
// I AM NOT DONE
1615

1716
use std::borrow::Cow;
1817

@@ -48,7 +47,8 @@ mod tests {
4847
let slice = [0, 1, 2];
4948
let mut input = Cow::from(&slice[..]);
5049
match abs_all(&mut input) {
51-
// TODO
50+
Cow::Borrowed(_) => Ok(()),
51+
_ => Err("Expected borrowed value"),
5252
}
5353
}
5454

@@ -60,7 +60,8 @@ mod tests {
6060
let slice = vec![0, 1, 2];
6161
let mut input = Cow::from(slice);
6262
match abs_all(&mut input) {
63-
// TODO
63+
Cow::Owned(_) => Ok(()),
64+
_ => Err("Expected borrowed or mutated value"),
6465
}
6566
}
6667

@@ -72,7 +73,8 @@ mod tests {
7273
let slice = vec![-1, 0, 1];
7374
let mut input = Cow::from(slice);
7475
match abs_all(&mut input) {
75-
// TODO
76+
Cow::Owned(_) => Ok(()),
77+
_ => Err("Expected mutated value"),
7678
}
7779
}
7880
}

exercises/smart_pointers/rc1.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
1212

13-
// I AM NOT DONE
14-
1513
use std::rc::Rc;
1614

1715
#[derive(Debug)]
@@ -60,17 +58,17 @@ fn main() {
6058
jupiter.details();
6159

6260
// TODO
63-
let saturn = Planet::Saturn(Rc::new(Sun {}));
61+
let saturn = Planet::Saturn(Rc::clone(&sun));
6462
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
6563
saturn.details();
6664

6765
// TODO
68-
let uranus = Planet::Uranus(Rc::new(Sun {}));
66+
let uranus = Planet::Uranus(Rc::clone(&sun));
6967
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
7068
uranus.details();
7169

7270
// TODO
73-
let neptune = Planet::Neptune(Rc::new(Sun {}));
71+
let neptune = Planet::Neptune(Rc::clone(&sun));
7472
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
7573
neptune.details();
7674

@@ -92,12 +90,15 @@ fn main() {
9290
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
9391

9492
// TODO
93+
drop(earth);
9594
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
9695

9796
// TODO
97+
drop(venus);
9898
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
9999

100100
// TODO
101+
drop(mercury);
101102
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
102103

103104
assert_eq!(Rc::strong_count(&sun), 1);

0 commit comments

Comments
 (0)