record progress: smart pointers

This commit is contained in:
Denis-Cosmin Nutiu 2024-11-05 23:52:29 +02:00
parent 0d6e6da436
commit 8f317fa4dd
5 changed files with 60 additions and 17 deletions

View file

@ -23,13 +23,13 @@ fn main() {
let numbers: Vec<_> = (0..100u32).collect(); let numbers: Vec<_> = (0..100u32).collect();
// TODO: Define `shared_numbers` by using `Arc`. // TODO: Define `shared_numbers` by using `Arc`.
// let shared_numbers = ???; let shared_numbers = Arc::new(numbers);
let mut join_handles = Vec::new(); let mut join_handles = Vec::new();
for offset in 0..8 { for offset in 0..8 {
// TODO: Define `child_numbers` using `shared_numbers`. // TODO: Define `child_numbers` using `shared_numbers`.
// let child_numbers = ???; let child_numbers = shared_numbers.clone();
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum(); let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();

View file

@ -12,18 +12,18 @@
// TODO: Use a `Box` in the enum definition to make the code compile. // TODO: Use a `Box` in the enum definition to make the code compile.
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
enum List { enum List {
Cons(i32, List), Cons(i32, Box<List>),
Nil, Nil,
} }
// TODO: Create an empty cons list. // TODO: Create an empty cons list.
fn create_empty_list() -> List { fn create_empty_list() -> List {
todo!() List::Nil
} }
// TODO: Create a non-empty cons list. // TODO: Create a non-empty cons list.
fn create_non_empty_list() -> List { fn create_non_empty_list() -> List {
todo!() List::Cons(20, Box::new(List::Nil))
} }
fn main() { fn main() {

View file

@ -39,7 +39,7 @@ mod tests {
let mut input = Cow::from(&vec); let mut input = Cow::from(&vec);
abs_all(&mut input); abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`. // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!())); assert!(matches!(input, Cow::Borrowed(_)));
} }
#[test] #[test]
@ -52,7 +52,7 @@ mod tests {
let mut input = Cow::from(vec); let mut input = Cow::from(vec);
abs_all(&mut input); abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`. // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!())); assert!(matches!(input, Cow::Owned(_)));
} }
#[test] #[test]
@ -64,6 +64,6 @@ mod tests {
let mut input = Cow::from(vec); let mut input = Cow::from(vec);
abs_all(&mut input); abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`. // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!())); assert!(matches!(input, Cow::Owned(_)));
} }
} }

View file

@ -61,17 +61,17 @@ mod tests {
jupiter.details(); jupiter.details();
// TODO // TODO
let saturn = Planet::Saturn(Rc::new(Sun)); let saturn = Planet::Saturn(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
saturn.details(); saturn.details();
// TODO // TODO
let uranus = Planet::Uranus(Rc::new(Sun)); let uranus = Planet::Uranus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
uranus.details(); uranus.details();
// TODO // TODO
let neptune = Planet::Neptune(Rc::new(Sun)); let neptune = Planet::Neptune(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
neptune.details(); neptune.details();
@ -92,13 +92,13 @@ mod tests {
drop(mars); drop(mars);
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
// TODO drop(venus);
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
// TODO drop(mercury);
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
// TODO drop(earth);
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
assert_eq!(Rc::strong_count(&sun), 1); assert_eq!(Rc::strong_count(&sun), 1);

View file

@ -1,4 +1,47 @@
fn main() { // At compile time, Rust needs to know how much space a type takes up. This
// DON'T EDIT THIS SOLUTION FILE! // becomes problematic for recursive types, where a value can have as part of
// It will be automatically filled after you finish the exercise. // itself another value of the same type. To get around the issue, we can use a
// `Box` - a smart pointer used to store data on the heap, which also allows us
// to wrap a recursive type.
//
// The recursive type we're implementing in this exercise is the "cons list", a
// data structure frequently found in functional programming languages. Each
// item in a cons list contains two elements: The value of the current item and
// the next item. The last item is a value called `Nil`.
#[derive(PartialEq, Debug)]
enum List {
Cons(i32, Box<List>),
Nil,
}
fn create_empty_list() -> List {
List::Nil
}
fn create_non_empty_list() -> List {
List::Cons(42, Box::new(List::Nil))
}
fn main() {
println!("This is an empty cons list: {:?}", create_empty_list());
println!(
"This is a non-empty cons list: {:?}",
create_non_empty_list(),
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_empty_list() {
assert_eq!(create_empty_list(), List::Nil);
}
#[test]
fn test_create_non_empty_list() {
assert_ne!(create_empty_list(), create_non_empty_list());
}
} }