From b4fed4710484da5dd762bf6d1dd0c805487f3809 Mon Sep 17 00:00:00 2001 From: Esteban Escobar Date: Sat, 4 Feb 2023 15:26:26 -0500 Subject: [PATCH] onto threads just finished Cow --- REVISITS.md | 6 +++++- exercises/standard_library_types/arc1.rs | 6 ++---- exercises/standard_library_types/box1.rs | 8 +++----- exercises/standard_library_types/cow1.rs | 3 +-- exercises/standard_library_types/iterators5.rs | 6 ++---- exercises/standard_library_types/rc1.rs | 10 ++++++---- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/REVISITS.md b/REVISITS.md index 05bb5649..b79030fd 100644 --- a/REVISITS.md +++ b/REVISITS.md @@ -26,6 +26,10 @@ return results to new Vec vector 27) lifetimes1 28) standard library types iterators2 -> VERY IMPORTANT to understand map, chain/chain::>, join 29) iterator4 to make sure you understand fold/rfold +30) iterator5 to make sure you understand looping through hashmaps with filter, flat_map, count +31) box.rs for smart pointer to allocate memory in recursive types +32) arc.rs for atomic reference counters, moving ownershup to thread::spawn. +33) rc.rs Rc::strong_count, Rc::new and Rc::clone, and drop(). Also note "In summary, if you need shared ownership in a single-threaded context, you can use Rc. If you need shared ownership across multiple threads, you should use Arc." Rc is faster and smaller than Arc -clippy conversions macros standard_library_types threads +clippy conversions macros threads diff --git a/exercises/standard_library_types/arc1.rs b/exercises/standard_library_types/arc1.rs index 93a27036..b35a1bcc 100644 --- a/exercises/standard_library_types/arc1.rs +++ b/exercises/standard_library_types/arc1.rs @@ -18,19 +18,17 @@ // where the second TODO comment is. Try not to create any copies of the `numbers` Vec! // Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #![forbid(unused_imports)] // Do not change this, (or the next) line. use std::sync::Arc; 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); diff --git a/exercises/standard_library_types/box1.rs b/exercises/standard_library_types/box1.rs index 66cf00f3..00e0bef3 100644 --- a/exercises/standard_library_types/box1.rs +++ b/exercises/standard_library_types/box1.rs @@ -16,11 +16,9 @@ // // Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[derive(PartialEq, Debug)] pub enum List { - Cons(i32, List), + Cons(i32, Box), Nil, } @@ -33,11 +31,11 @@ fn main() { } pub fn create_empty_list() -> List { - todo!() + List::Nil } pub fn create_non_empty_list() -> List { - todo!() + List::Cons(1, Box::new(List::Nil)) } #[cfg(test)] diff --git a/exercises/standard_library_types/cow1.rs b/exercises/standard_library_types/cow1.rs index 5fba2519..318e0efa 100644 --- a/exercises/standard_library_types/cow1.rs +++ b/exercises/standard_library_types/cow1.rs @@ -5,7 +5,6 @@ // It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required. // The type is designed to work with general borrowed data via the Borrow trait. -// I AM NOT DONE use std::borrow::Cow; @@ -42,7 +41,7 @@ fn main() { let mut input = Cow::from(slice); match abs_all(&mut input) { // TODO - Cow::Borrowed(_) => println!("I own this slice!"), + Cow::Owned(_) => println!("I own this slice!"), _ => panic!("expected borrowed value"), } } diff --git a/exercises/standard_library_types/iterators5.rs b/exercises/standard_library_types/iterators5.rs index 0593d123..d36e3564 100644 --- a/exercises/standard_library_types/iterators5.rs +++ b/exercises/standard_library_types/iterators5.rs @@ -10,8 +10,6 @@ // // Make the code compile and the tests pass. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Clone, Copy, PartialEq, Eq)] @@ -34,7 +32,7 @@ fn count_for(map: &HashMap, value: Progress) -> usize { fn count_iterator(map: &HashMap, value: Progress) -> usize { // map is a hashmap with String keys and Progress values. // map = { "variables1": Complete, "from_str": None, ... } - todo!(); + map.iter().filter(|(key, val)| **val == value).count() } fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { @@ -53,7 +51,7 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr // collection is a slice of hashmaps. // collection = [{ "variables1": Complete, "from_str": None, ... }, // { "variables2": Complete, ... }, ... ] - todo!(); + collection.iter().flat_map(|map| map.values()).filter(|val| **val == value).count() } #[cfg(test)] diff --git a/exercises/standard_library_types/rc1.rs b/exercises/standard_library_types/rc1.rs index 9b907fde..c02d1f89 100644 --- a/exercises/standard_library_types/rc1.rs +++ b/exercises/standard_library_types/rc1.rs @@ -5,7 +5,6 @@ // Make this code compile by using the proper Rc primitives to express that the sun has multiple owners. -// I AM NOT DONE use std::rc::Rc; #[derive(Debug)] @@ -54,17 +53,17 @@ fn main() { 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(); @@ -86,12 +85,15 @@ fn main() { 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);