summaryrefslogtreecommitdiff
path: root/rustlings
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings')
-rw-r--r--rustlings/.rustlings-state.txt8
-rw-r--r--rustlings/exercises/19_smart_pointers/arc1.rs4
-rw-r--r--rustlings/exercises/19_smart_pointers/box1.rs6
-rw-r--r--rustlings/exercises/19_smart_pointers/cow1.rs9
-rw-r--r--rustlings/exercises/19_smart_pointers/rc1.rs9
-rw-r--r--rustlings/solutions/19_smart_pointers/arc1.rs45
-rw-r--r--rustlings/solutions/19_smart_pointers/box1.rs47
-rw-r--r--rustlings/solutions/19_smart_pointers/cow1.rs69
-rw-r--r--rustlings/solutions/19_smart_pointers/rc1.rs102
9 files changed, 275 insertions, 24 deletions
diff --git a/rustlings/.rustlings-state.txt b/rustlings/.rustlings-state.txt
index 56f1a68..7523b62 100644
--- a/rustlings/.rustlings-state.txt
+++ b/rustlings/.rustlings-state.txt
@@ -1,6 +1,6 @@
DON'T EDIT THIS FILE!
-box1
+threads1
intro1
intro2
@@ -76,4 +76,8 @@ iterators1
iterators2
iterators3
iterators4
-iterators5 \ No newline at end of file
+iterators5
+box1
+rc1
+arc1
+cow1 \ No newline at end of file
diff --git a/rustlings/exercises/19_smart_pointers/arc1.rs b/rustlings/exercises/19_smart_pointers/arc1.rs
index 6bb860f..f44e4e1 100644
--- a/rustlings/exercises/19_smart_pointers/arc1.rs
+++ b/rustlings/exercises/19_smart_pointers/arc1.rs
@@ -23,13 +23,13 @@ fn main() {
let numbers: Vec<_> = (0..100u32).collect();
// TODO: Define `shared_numbers` by using `Arc`.
- // let shared_numbers = ???;
+ let shared_numbers = Arc::new(numbers);
let mut join_handles = Vec::new();
for offset in 0..8 {
// TODO: Define `child_numbers` using `shared_numbers`.
- // let child_numbers = ???;
+ let child_numbers = shared_numbers.clone();
let handle = thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
diff --git a/rustlings/exercises/19_smart_pointers/box1.rs b/rustlings/exercises/19_smart_pointers/box1.rs
index d70e1c3..ee379ca 100644
--- a/rustlings/exercises/19_smart_pointers/box1.rs
+++ b/rustlings/exercises/19_smart_pointers/box1.rs
@@ -12,18 +12,18 @@
// TODO: Use a `Box` in the enum definition to make the code compile.
#[derive(PartialEq, Debug)]
enum List {
- Cons(i32, List),
+ Cons(i32, Box<List>),
Nil,
}
// TODO: Create an empty cons list.
fn create_empty_list() -> List {
- todo!()
+ List::Nil
}
// TODO: Create a non-empty cons list.
fn create_non_empty_list() -> List {
- todo!()
+ List::Cons(1, Box::new(List::Nil))
}
fn main() {
diff --git a/rustlings/exercises/19_smart_pointers/cow1.rs b/rustlings/exercises/19_smart_pointers/cow1.rs
index 1566500..56a25b9 100644
--- a/rustlings/exercises/19_smart_pointers/cow1.rs
+++ b/rustlings/exercises/19_smart_pointers/cow1.rs
@@ -38,8 +38,7 @@ mod tests {
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!()));
+ assert!(matches!(input, Cow::Borrowed(_)));
}
#[test]
@@ -51,8 +50,7 @@ mod tests {
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!()));
+ assert!(matches!(input, Cow::Owned(_)));
}
#[test]
@@ -63,7 +61,6 @@ mod tests {
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!()));
+ assert!(matches!(input, Cow::Owned(_)));
}
}
diff --git a/rustlings/exercises/19_smart_pointers/rc1.rs b/rustlings/exercises/19_smart_pointers/rc1.rs
index ecd3438..5c9ce84 100644
--- a/rustlings/exercises/19_smart_pointers/rc1.rs
+++ b/rustlings/exercises/19_smart_pointers/rc1.rs
@@ -60,17 +60,17 @@ mod tests {
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();
@@ -92,12 +92,15 @@ mod tests {
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);
diff --git a/rustlings/solutions/19_smart_pointers/arc1.rs b/rustlings/solutions/19_smart_pointers/arc1.rs
index dcf2377..bd76189 100644
--- a/rustlings/solutions/19_smart_pointers/arc1.rs
+++ b/rustlings/solutions/19_smart_pointers/arc1.rs
@@ -1,4 +1,45 @@
+// 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.
+//
+// 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`.
+//
+// Don't get distracted by how threads are spawned and joined. We will practice
+// that later in the exercises about threads.
+
+// Don't change the lines below.
+#![forbid(unused_imports)]
+use std::{sync::Arc, thread};
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ let numbers: Vec<_> = (0..100u32).collect();
+
+ let shared_numbers = Arc::new(numbers);
+ // ^^^^^^^^^^^^^^^^^
+
+ let mut join_handles = Vec::new();
+
+ for offset in 0..8 {
+ let child_numbers = Arc::clone(&shared_numbers);
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+ let handle = thread::spawn(move || {
+ let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
+ println!("Sum of offset {offset} is {sum}");
+ });
+
+ join_handles.push(handle);
+ }
+
+ for handle in join_handles.into_iter() {
+ handle.join().unwrap();
+ }
}
diff --git a/rustlings/solutions/19_smart_pointers/box1.rs b/rustlings/solutions/19_smart_pointers/box1.rs
index dcf2377..189cc56 100644
--- a/rustlings/solutions/19_smart_pointers/box1.rs
+++ b/rustlings/solutions/19_smart_pointers/box1.rs
@@ -1,4 +1,47 @@
+// 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
+// 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() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ 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());
+ }
}
diff --git a/rustlings/solutions/19_smart_pointers/cow1.rs b/rustlings/solutions/19_smart_pointers/cow1.rs
index dcf2377..461143b 100644
--- a/rustlings/solutions/19_smart_pointers/cow1.rs
+++ b/rustlings/solutions/19_smart_pointers/cow1.rs
@@ -1,4 +1,69 @@
+// 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(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()[ind] = -value;
+ }
+ }
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference_mutation() {
+ // Clone occurs because `input` needs to be mutated.
+ 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() {
+ // No clone occurs because `input` doesn't need to be mutated.
+ let vec = vec![0, 1, 2];
+ let mut input = Cow::from(&vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Borrowed(_)));
+ // ^^^^^^^^^^^^^^^^
+ }
+
+ #[test]
+ fn owned_no_mutation() {
+ // We can also pass `vec` without `&` so `Cow` owns it directly. In this
+ // case, no mutation occurs (all numbers are already absolute) and thus
+ // also no clone. But the result is still owned because it was never
+ // borrowed or mutated.
+ let vec = vec![0, 1, 2];
+ let mut input = Cow::from(vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Owned(_)));
+ // ^^^^^^^^^^^^^
+ }
+
+ #[test]
+ fn owned_mutation() {
+ // Of course this is also the case if a mutation does occur (not all
+ // numbers are absolute). In this case, the call to `to_mut()` in the
+ // `abs_all` function returns a reference to the same data as before.
+ let vec = vec![-1, 0, 1];
+ let mut input = Cow::from(vec);
+ abs_all(&mut input);
+ assert!(matches!(input, Cow::Owned(_)));
+ // ^^^^^^^^^^^^^
+ }
}
diff --git a/rustlings/solutions/19_smart_pointers/rc1.rs b/rustlings/solutions/19_smart_pointers/rc1.rs
index dcf2377..edf40eb 100644
--- a/rustlings/solutions/19_smart_pointers/rc1.rs
+++ b/rustlings/solutions/19_smart_pointers/rc1.rs
@@ -1,4 +1,102 @@
+// 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 `Planet`s. The planets take ownership of the sun, indicating that
+// they revolve around the sun.
+
+use std::rc::Rc;
+
+#[derive(Debug)]
+struct Sun;
+
+#[derive(Debug)]
+enum Planet {
+ Mercury(Rc<Sun>),
+ Venus(Rc<Sun>),
+ Earth(Rc<Sun>),
+ Mars(Rc<Sun>),
+ Jupiter(Rc<Sun>),
+ Saturn(Rc<Sun>),
+ Uranus(Rc<Sun>),
+ Neptune(Rc<Sun>),
+}
+
+impl Planet {
+ fn details(&self) {
+ println!("Hi from {self:?}!");
+ }
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn rc1() {
+ let sun = Rc::new(Sun);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+
+ let mercury = Planet::Mercury(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
+ mercury.details();
+
+ let venus = Planet::Venus(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
+ venus.details();
+
+ let earth = Planet::Earth(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
+ earth.details();
+
+ let mars = Planet::Mars(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
+ mars.details();
+
+ let jupiter = Planet::Jupiter(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
+ jupiter.details();
+
+ let saturn = Planet::Saturn(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
+ saturn.details();
+
+ let uranus = Planet::Uranus(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
+ uranus.details();
+
+ let neptune = Planet::Neptune(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
+ neptune.details();
+
+ assert_eq!(Rc::strong_count(&sun), 9);
+
+ drop(neptune);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
+
+ drop(uranus);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
+
+ drop(saturn);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
+
+ drop(jupiter);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
+
+ drop(mars);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
+
+ drop(earth);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
+
+ drop(venus);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
+
+ drop(mercury);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+
+ assert_eq!(Rc::strong_count(&sun), 1);
+ }
}