summaryrefslogtreecommitdiff
path: root/rustlings/exercises/19_smart_pointers/arc1.rs
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-09-14 17:15:13 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-09-14 17:28:26 -0500
commitbf66df1a16461ef1ee50510803a3097914ffcb82 (patch)
tree15a13e34a794ec182c1e4c4507645ac43e07c6bc /rustlings/exercises/19_smart_pointers/arc1.rs
parentb6245506460bd854ab32b1b6d2ecc51f5be82ef9 (diff)
Add rustlings
Diffstat (limited to 'rustlings/exercises/19_smart_pointers/arc1.rs')
-rw-r--r--rustlings/exercises/19_smart_pointers/arc1.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/rustlings/exercises/19_smart_pointers/arc1.rs b/rustlings/exercises/19_smart_pointers/arc1.rs
new file mode 100644
index 0000000..6bb860f
--- /dev/null
+++ b/rustlings/exercises/19_smart_pointers/arc1.rs
@@ -0,0 +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() {
+ let numbers: Vec<_> = (0..100u32).collect();
+
+ // TODO: Define `shared_numbers` by using `Arc`.
+ // let shared_numbers = ???;
+
+ let mut join_handles = Vec::new();
+
+ for offset in 0..8 {
+ // 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 {offset} is {sum}");
+ });
+
+ join_handles.push(handle);
+ }
+
+ for handle in join_handles.into_iter() {
+ handle.join().unwrap();
+ }
+}