summaryrefslogtreecommitdiff
path: root/exercises/19_smart_pointers
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
committermo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
commit7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch)
treec67f7e62bb9a179ae4fdbab492501cb6847e64c7 /exercises/19_smart_pointers
parent77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff)
parent4d9c346a173bb722b929f3ea3c00f84954483e24 (diff)
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'exercises/19_smart_pointers')
-rw-r--r--exercises/19_smart_pointers/arc1.rs62
-rw-r--r--exercises/19_smart_pointers/box1.rs42
-rw-r--r--exercises/19_smart_pointers/cow1.rs88
-rw-r--r--exercises/19_smart_pointers/rc1.rs125
4 files changed, 150 insertions, 167 deletions
diff --git a/exercises/19_smart_pointers/arc1.rs b/exercises/19_smart_pointers/arc1.rs
index 3526ddc..6bb860f 100644
--- a/exercises/19_smart_pointers/arc1.rs
+++ b/exercises/19_smart_pointers/arc1.rs
@@ -1,45 +1,45 @@
-// arc1.rs
+// In this exercise, we are given a `Vec` of `u32` called `numbers` with values
+// ranging from 0 to 99. We would like to use this set of numbers within 8
+// different threads simultaneously. Each thread is going to get the sum of
+// every eighth value with an offset.
//
-// In this exercise, we are given a Vec of u32 called "numbers" with values
-// ranging from 0 to 99 -- [ 0, 1, 2, ..., 98, 99 ] We would like to use this
-// set of numbers within 8 different threads simultaneously. Each thread is
-// going to get the sum of every eighth value, with an offset.
+// The first thread (offset 0), will sum 0, 8, 16, …
+// The second thread (offset 1), will sum 1, 9, 17, …
+// The third thread (offset 2), will sum 2, 10, 18, …
+// …
+// The eighth thread (offset 7), will sum 7, 15, 23, …
//
-// The first thread (offset 0), will sum 0, 8, 16, ...
-// The second thread (offset 1), will sum 1, 9, 17, ...
-// The third thread (offset 2), will sum 2, 10, 18, ...
-// ...
-// The eighth thread (offset 7), will sum 7, 15, 23, ...
+// Each thread should own a reference-counting pointer to the vector of
+// numbers. But `Rc` isn't thread-safe. Therefore, we need to use `Arc`.
//
-// Because we are using threads, our values need to be thread-safe. Therefore,
-// we are using Arc. We need to make a change in each of the two TODOs.
-//
-// Make this code compile by filling in a value for `shared_numbers` where the
-// first TODO comment is, and create an initial binding for `child_numbers`
-// 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.
+// Don't get distracted by how threads are spawned and joined. We will practice
+// that later in the exercises about threads.
-// I AM NOT DONE
-
-#![forbid(unused_imports)] // Do not change this, (or the next) line.
-use std::sync::Arc;
-use std::thread;
+// Don't change the lines below.
+#![forbid(unused_imports)]
+use std::{sync::Arc, thread};
fn main() {
let numbers: Vec<_> = (0..100u32).collect();
- let shared_numbers = // TODO
- let mut joinhandles = Vec::new();
+
+ // TODO: Define `shared_numbers` by using `Arc`.
+ // let shared_numbers = ???;
+
+ let mut join_handles = Vec::new();
for offset in 0..8 {
- let child_numbers = // TODO
- joinhandles.push(thread::spawn(move || {
+ // TODO: Define `child_numbers` using `shared_numbers`.
+ // let child_numbers = ???;
+
+ let handle = thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
- println!("Sum of offset {} is {}", offset, sum);
- }));
+ println!("Sum of offset {offset} is {sum}");
+ });
+
+ join_handles.push(handle);
}
- for handle in joinhandles.into_iter() {
+
+ for handle in join_handles.into_iter() {
handle.join().unwrap();
}
}
diff --git a/exercises/19_smart_pointers/box1.rs b/exercises/19_smart_pointers/box1.rs
index 513e7da..d70e1c3 100644
--- a/exercises/19_smart_pointers/box1.rs
+++ b/exercises/19_smart_pointers/box1.rs
@@ -1,45 +1,37 @@
-// box1.rs
-//
// At compile time, Rust needs to know how much space a type takes up. This
// becomes problematic for recursive types, where a value can have as part of
// 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
+// 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
+// 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`.
-//
-// Step 1: use a `Box` in the enum definition to make the code compile
-// Step 2: create both empty and non-empty cons lists by replacing `todo!()`
-//
-// Note: the tests should not be changed
-//
-// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
-
-// I AM NOT DONE
+// TODO: Use a `Box` in the enum definition to make the code compile.
#[derive(PartialEq, Debug)]
-pub enum List {
+enum List {
Cons(i32, 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()
- );
+// TODO: Create an empty cons list.
+fn create_empty_list() -> List {
+ todo!()
}
-pub fn create_empty_list() -> List {
+// TODO: Create a non-empty cons list.
+fn create_non_empty_list() -> List {
todo!()
}
-pub fn create_non_empty_list() -> List {
- todo!()
+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)]
@@ -48,11 +40,11 @@ mod tests {
#[test]
fn test_create_empty_list() {
- assert_eq!(List::Nil, 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())
+ assert_ne!(create_empty_list(), create_non_empty_list());
}
}
diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs
index fcd3e0b..5ecf848 100644
--- a/exercises/19_smart_pointers/cow1.rs
+++ b/exercises/19_smart_pointers/cow1.rs
@@ -1,30 +1,22 @@
-// cow1.rs
-//
-// This exercise explores the Cow, or Clone-On-Write type. Cow is a
-// clone-on-write smart pointer. 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.
-//
-// This exercise is meant to show you what to expect when passing data to Cow.
-// Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the
-// TODO markers.
-//
-// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
-
-// I AM NOT DONE
+// This exercise explores the `Cow` (Clone-On-Write) smart pointer. 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.
use std::borrow::Cow;
-fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
- for i in 0..input.len() {
- let v = input[i];
- if v < 0 {
+fn abs_all(input: &mut Cow<[i32]>) {
+ for ind in 0..input.len() {
+ let value = input[ind];
+ if value < 0 {
// Clones into a vector if not already owned.
- input.to_mut()[i] = -v;
+ input.to_mut()[ind] = -value;
}
}
- input
+}
+
+fn main() {
+ // You can optionally experiment here.
}
#[cfg(test)]
@@ -32,47 +24,45 @@ mod tests {
use super::*;
#[test]
- fn reference_mutation() -> Result<(), &'static str> {
+ fn reference_mutation() {
// Clone occurs because `input` needs to be mutated.
- let slice = [-1, 0, 1];
- let mut input = Cow::from(&slice[..]);
- match abs_all(&mut input) {
- Cow::Owned(_) => Ok(()),
- _ => Err("Expected owned value"),
- }
+ let vec = vec![-1, 0, 1];
+ let mut input = Cow::from(&vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Owned(_)));
}
#[test]
- fn reference_no_mutation() -> Result<(), &'static str> {
+ fn reference_no_mutation() {
// No clone occurs because `input` doesn't need to be mutated.
- let slice = [0, 1, 2];
- let mut input = Cow::from(&slice[..]);
- match abs_all(&mut input) {
- // TODO
- }
+ 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!()));
}
#[test]
- fn owned_no_mutation() -> Result<(), &'static str> {
- // We can also pass `slice` without `&` so Cow owns it directly. In this
- // case no mutation occurs and thus also no clone, but the result is
+ fn owned_no_mutation() {
+ // We can also pass `vec` without `&` so `Cow` owns it directly. In this
+ // case, no mutation occurs and thus also no clone. But the result is
// still owned because it was never borrowed or mutated.
- let slice = vec![0, 1, 2];
- let mut input = Cow::from(slice);
- match abs_all(&mut input) {
- // TODO
- }
+ 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!()));
}
#[test]
- fn owned_mutation() -> Result<(), &'static str> {
+ fn owned_mutation() {
// Of course this is also the case if a mutation does occur. In this
- // case the call to `to_mut()` in the abs_all() function returns a
+ // case, the call to `to_mut()` in the `abs_all` function returns a
// reference to the same data as before.
- let slice = vec![-1, 0, 1];
- let mut input = Cow::from(slice);
- match abs_all(&mut input) {
- // TODO
- }
+ 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!()));
}
}
diff --git a/exercises/19_smart_pointers/rc1.rs b/exercises/19_smart_pointers/rc1.rs
index 1b90346..48e19dc 100644
--- a/exercises/19_smart_pointers/rc1.rs
+++ b/exercises/19_smart_pointers/rc1.rs
@@ -1,22 +1,14 @@
-// rc1.rs
-//
// In this exercise, we want to express the concept of multiple owners via the
-// Rc<T> type. This is a model of our solar system - there is a Sun type and
-// multiple Planets. The Planets take ownership of the sun, indicating that they
-// revolve around the sun.
-//
-// Make this code compile by using the proper Rc primitives to express that the
-// sun has multiple owners.
-//
-// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
-
-// I AM NOT DONE
+// `Rc<T>` type. This is a model of our solar system - there is a `Sun` type and
+// multiple `Planet`s. The planets take ownership of the sun, indicating that
+// they revolve around the sun.
use std::rc::Rc;
#[derive(Debug)]
-struct Sun {}
+struct Sun;
+#[allow(dead_code)]
#[derive(Debug)]
enum Planet {
Mercury(Rc<Sun>),
@@ -31,75 +23,84 @@ enum Planet {
impl Planet {
fn details(&self) {
- println!("Hi from {:?}!", self)
+ println!("Hi from {self:?}!");
}
}
-#[test]
fn main() {
- let sun = Rc::new(Sun {});
- println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
- let mercury = Planet::Mercury(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
- mercury.details();
+ #[test]
+ fn rc1() {
+ let sun = Rc::new(Sun);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
- let venus = Planet::Venus(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
- venus.details();
+ let mercury = Planet::Mercury(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
+ mercury.details();
- let earth = Planet::Earth(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
- earth.details();
+ let venus = Planet::Venus(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
+ venus.details();
- let mars = Planet::Mars(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
- mars.details();
+ let earth = Planet::Earth(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
+ earth.details();
- let jupiter = Planet::Jupiter(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
- jupiter.details();
+ let mars = Planet::Mars(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
+ mars.details();
- // TODO
- let saturn = Planet::Saturn(Rc::new(Sun {}));
- println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
- saturn.details();
+ let jupiter = Planet::Jupiter(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
+ jupiter.details();
- // TODO
- let uranus = Planet::Uranus(Rc::new(Sun {}));
- println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
- uranus.details();
+ // TODO
+ let saturn = Planet::Saturn(Rc::new(Sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
+ saturn.details();
- // TODO
- let neptune = Planet::Neptune(Rc::new(Sun {}));
- println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
- neptune.details();
+ // TODO
+ let uranus = Planet::Uranus(Rc::new(Sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
+ uranus.details();
- assert_eq!(Rc::strong_count(&sun), 9);
+ // TODO
+ let neptune = Planet::Neptune(Rc::new(Sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
+ neptune.details();
- drop(neptune);
- println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
+ assert_eq!(Rc::strong_count(&sun), 9);
- drop(uranus);
- println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
+ drop(neptune);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
- drop(saturn);
- println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
+ drop(uranus);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
- drop(jupiter);
- println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
+ drop(saturn);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
- drop(mars);
- println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
+ drop(jupiter);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
- // TODO
- println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
+ drop(mars);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
- // TODO
- println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
+ // TODO
+ println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
- // TODO
- println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+ // TODO
+ println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
- assert_eq!(Rc::strong_count(&sun), 1);
+ // TODO
+ println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+
+ assert_eq!(Rc::strong_count(&sun), 1);
+ }
}