Skip to content

Commit

Permalink
finish smart_pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Withsan committed Oct 7, 2023
1 parent c3fd2d0 commit b0fa445
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions exercises/smart_pointers/arc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use std::thread;

fn main() {
let numbers: Vec<_> = (0..100u32).collect();
let shared_numbers = // TODO
let shared_numbers = Arc::new(numbers);
let mut joinhandles = Vec::new();

for offset in 0..8 {
let child_numbers = // TODO
let child_numbers = shared_numbers.clone();
joinhandles.push(thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
println!("Sum of offset {} is {}", offset, sum);
Expand Down
6 changes: 3 additions & 3 deletions exercises/smart_pointers/box1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#[derive(PartialEq, Debug)]
pub enum List {
Cons(i32, List),
Cons(i32, Box<List>),
Nil,
}

Expand All @@ -35,11 +35,11 @@ fn main() {
}

pub fn create_empty_list() -> List {
todo!()
List::Nil
}

pub fn create_non_empty_list() -> List {
todo!()
List::Cons(32, Box::new(List::Nil))
}

#[cfg(test)]
Expand Down
12 changes: 9 additions & 3 deletions exercises/smart_pointers/cow1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ mod tests {
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => Ok(()),
_ => Err("Expected borrowed value"),
}
}

Expand All @@ -60,7 +61,8 @@ mod tests {
let slice = vec![0, 1, 2];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}

Expand All @@ -72,7 +74,11 @@ mod tests {
let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(s) => {
println!("{:?}", s);
Ok(())
}
_ => Err("Expected owned value"),
}
}
}
15 changes: 6 additions & 9 deletions exercises/smart_pointers/rc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,15 @@ fn main() {
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
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();

Expand All @@ -92,13 +89,13 @@ fn main() {
drop(mars);
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);
Expand Down

0 comments on commit b0fa445

Please sign in to comment.