We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5d80f26 commit 051c3aeCopy full SHA for 051c3ae
exercises/smart_pointers/arc1.rs
@@ -21,19 +21,17 @@
21
//
22
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
23
24
-// I AM NOT DONE
25
-
26
#![forbid(unused_imports)] // Do not change this, (or the next) line.
27
use std::sync::Arc;
28
use std::thread;
29
30
fn main() {
31
let numbers: Vec<_> = (0..100u32).collect();
32
- let shared_numbers = // TODO
+ let shared_numbers = Arc::new(numbers);
33
let mut joinhandles = Vec::new();
34
35
for offset in 0..8 {
36
- let child_numbers = // TODO
+ let child_numbers = Arc::clone(&shared_numbers);
37
joinhandles.push(thread::spawn(move || {
38
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
39
println!("Sum of offset {} is {}", offset, sum);
exercises/smart_pointers/box1.rs
@@ -18,11 +18,9 @@
18
19
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
20
#[derive(PartialEq, Debug)]
pub enum List {
- Cons(i32, List),
+ Cons(i32, Box<List> ),
Nil,
}
@@ -35,11 +33,11 @@ fn main() {
pub fn create_empty_list() -> List {
- todo!()
+ List::Nil
40
41
pub fn create_non_empty_list() -> List {
42
+ List::Cons(114514, Box::new(List::Nil) )
43
44
45
#[cfg(test)]
exercises/smart_pointers/cow1.rs
@@ -12,8 +12,6 @@
12
13
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
14
15
16
17
use std::borrow::Cow;
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
@@ -48,7 +46,8 @@ mod tests {
48
46
let slice = [0, 1, 2];
49
47
let mut input = Cow::from(&slice[..]);
50
match abs_all(&mut input) {
51
- // TODO
+ Cow::Borrowed(_) => Ok(()),
+ _ => Err("Expected borrowed value")
52
53
54
@@ -60,7 +59,8 @@ mod tests {
60
59
let slice = vec![0, 1, 2];
61
let mut input = Cow::from(slice);
62
63
+ Cow::Owned(_) => Ok(()),
+ _ => Err("Expected owned value"),
64
65
66
@@ -72,7 +72,8 @@ mod tests {
72
let slice = vec![-1, 0, 1];
73
74
75
76
77
78
79
exercises/smart_pointers/rc1.rs
@@ -10,8 +10,6 @@
10
11
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
use std::rc::Rc;
#[derive(Debug)]
@@ -60,17 +58,17 @@ fn main() {
58
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();
67
68
- let uranus = Planet::Uranus(Rc::new(Sun {}));
+ let uranus = Planet::Uranus(Rc::clone(&sun));
69
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
70
uranus.details();
71
- 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();
@@ -91,13 +89,13 @@ fn main() {
91
89
drop(mars);
92
90
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
93
94
+ drop(earth);
95
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
96
97
+ drop(venus);
98
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
99
100
+ drop(mercury);
101
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
102
103
assert_eq!(Rc::strong_count(&sun), 1);
exercises/threads/threads1.rs
@@ -8,8 +8,6 @@
8
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
9
// hint.
use std::time::{Duration, Instant};
@@ -26,6 +24,8 @@ fn main() {
let mut results: Vec<u128> = vec![];
for handle in handles {
+ results.push(handle.join().unwrap());
+
// TODO: a struct is returned from thread::spawn, can you use it?
exercises/threads/threads2.rs
@@ -7,9 +7,7 @@
7
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
-use std::sync::Arc;
+use std::sync::{Arc, Mutex};
use std::time::Duration;
@@ -18,14 +16,15 @@ struct JobStatus {
- let status = Arc::new(JobStatus { jobs_completed: 0 });
+ let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let mut handles = vec![];
for _ in 0..10 {
let status_shared = Arc::clone(&status);
let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value
- status_shared.jobs_completed += 1;
+ let mut status = status_shared.lock().unwrap();
+ status.jobs_completed += 1;
});
handles.push(handle);
@@ -34,6 +33,6 @@ fn main() {
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
// anything interesting in the output? Do you have to 'join' on all the
// handles?
- println!("jobs completed {}", ???);
+ println!("jobs completed {}", status.lock().unwrap().jobs_completed);
exercises/threads/threads3.rs
@@ -3,8 +3,6 @@
3
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
4
5
6
use std::sync::mpsc;
@@ -31,6 +29,8 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc);
+ let tx1 = tx.clone();
thread::spawn(move || {
for val in &qc1.first_half {
println!("sending {:?}", val);
@@ -42,7 +42,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
for val in &qc2.second_half {
- tx.send(*val).unwrap();
+ tx1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
0 commit comments