Skip to content

Commit 051c3ae

Browse files
committed
update
1 parent 5d80f26 commit 051c3ae

File tree

7 files changed

+27
-33
lines changed

7 files changed

+27
-33
lines changed

exercises/smart_pointers/arc1.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@
2121
//
2222
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
2323

24-
// I AM NOT DONE
25-
2624
#![forbid(unused_imports)] // Do not change this, (or the next) line.
2725
use std::sync::Arc;
2826
use std::thread;
2927

3028
fn main() {
3129
let numbers: Vec<_> = (0..100u32).collect();
32-
let shared_numbers = // TODO
30+
let shared_numbers = Arc::new(numbers);
3331
let mut joinhandles = Vec::new();
3432

3533
for offset in 0..8 {
36-
let child_numbers = // TODO
34+
let child_numbers = Arc::clone(&shared_numbers);
3735
joinhandles.push(thread::spawn(move || {
3836
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
3937
println!("Sum of offset {} is {}", offset, sum);

exercises/smart_pointers/box1.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
//
1919
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
2020

21-
// I AM NOT DONE
22-
2321
#[derive(PartialEq, Debug)]
2422
pub enum List {
25-
Cons(i32, List),
23+
Cons(i32, Box<List> ),
2624
Nil,
2725
}
2826

@@ -35,11 +33,11 @@ fn main() {
3533
}
3634

3735
pub fn create_empty_list() -> List {
38-
todo!()
36+
List::Nil
3937
}
4038

4139
pub fn create_non_empty_list() -> List {
42-
todo!()
40+
List::Cons(114514, Box::new(List::Nil) )
4341
}
4442

4543
#[cfg(test)]

exercises/smart_pointers/cow1.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
//
1313
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
1414

15-
// I AM NOT DONE
16-
1715
use std::borrow::Cow;
1816

1917
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
@@ -48,7 +46,8 @@ mod tests {
4846
let slice = [0, 1, 2];
4947
let mut input = Cow::from(&slice[..]);
5048
match abs_all(&mut input) {
51-
// TODO
49+
Cow::Borrowed(_) => Ok(()),
50+
_ => Err("Expected borrowed value")
5251
}
5352
}
5453

@@ -60,7 +59,8 @@ mod tests {
6059
let slice = vec![0, 1, 2];
6160
let mut input = Cow::from(slice);
6261
match abs_all(&mut input) {
63-
// TODO
62+
Cow::Owned(_) => Ok(()),
63+
_ => Err("Expected owned value"),
6464
}
6565
}
6666

@@ -72,7 +72,8 @@ mod tests {
7272
let slice = vec![-1, 0, 1];
7373
let mut input = Cow::from(slice);
7474
match abs_all(&mut input) {
75-
// TODO
75+
Cow::Owned(_) => Ok(()),
76+
_ => Err("Expected owned value"),
7677
}
7778
}
7879
}

exercises/smart_pointers/rc1.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
1212

13-
// I AM NOT DONE
14-
1513
use std::rc::Rc;
1614

1715
#[derive(Debug)]
@@ -60,17 +58,17 @@ fn main() {
6058
jupiter.details();
6159

6260
// TODO
63-
let saturn = Planet::Saturn(Rc::new(Sun {}));
61+
let saturn = Planet::Saturn(Rc::clone(&sun));
6462
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
6563
saturn.details();
6664

6765
// TODO
68-
let uranus = Planet::Uranus(Rc::new(Sun {}));
66+
let uranus = Planet::Uranus(Rc::clone(&sun));
6967
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
7068
uranus.details();
7169

7270
// TODO
73-
let neptune = Planet::Neptune(Rc::new(Sun {}));
71+
let neptune = Planet::Neptune(Rc::clone(&sun));
7472
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
7573
neptune.details();
7674

@@ -91,13 +89,13 @@ fn main() {
9189
drop(mars);
9290
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
9391

94-
// TODO
92+
drop(earth);
9593
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
9694

97-
// TODO
95+
drop(venus);
9896
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
9997

100-
// TODO
98+
drop(mercury);
10199
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
102100

103101
assert_eq!(Rc::strong_count(&sun), 1);

exercises/threads/threads1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
12-
1311
use std::thread;
1412
use std::time::{Duration, Instant};
1513

@@ -26,6 +24,8 @@ fn main() {
2624

2725
let mut results: Vec<u128> = vec![];
2826
for handle in handles {
27+
results.push(handle.join().unwrap());
28+
2929
// TODO: a struct is returned from thread::spawn, can you use it?
3030
}
3131

exercises/threads/threads2.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
12-
use std::sync::Arc;
10+
use std::sync::{Arc, Mutex};
1311
use std::thread;
1412
use std::time::Duration;
1513

@@ -18,14 +16,15 @@ struct JobStatus {
1816
}
1917

2018
fn main() {
21-
let status = Arc::new(JobStatus { jobs_completed: 0 });
19+
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
2220
let mut handles = vec![];
2321
for _ in 0..10 {
2422
let status_shared = Arc::clone(&status);
2523
let handle = thread::spawn(move || {
2624
thread::sleep(Duration::from_millis(250));
2725
// TODO: You must take an action before you update a shared value
28-
status_shared.jobs_completed += 1;
26+
let mut status = status_shared.lock().unwrap();
27+
status.jobs_completed += 1;
2928
});
3029
handles.push(handle);
3130
}
@@ -34,6 +33,6 @@ fn main() {
3433
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
3534
// anything interesting in the output? Do you have to 'join' on all the
3635
// handles?
37-
println!("jobs completed {}", ???);
36+
println!("jobs completed {}", status.lock().unwrap().jobs_completed);
3837
}
3938
}

exercises/threads/threads3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
use std::sync::mpsc;
97
use std::sync::Arc;
108
use std::thread;
@@ -31,6 +29,8 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
3129
let qc1 = Arc::clone(&qc);
3230
let qc2 = Arc::clone(&qc);
3331

32+
let tx1 = tx.clone();
33+
3434
thread::spawn(move || {
3535
for val in &qc1.first_half {
3636
println!("sending {:?}", val);
@@ -42,7 +42,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
4242
thread::spawn(move || {
4343
for val in &qc2.second_half {
4444
println!("sending {:?}", val);
45-
tx.send(*val).unwrap();
45+
tx1.send(*val).unwrap();
4646
thread::sleep(Duration::from_secs(1));
4747
}
4848
});

0 commit comments

Comments
 (0)