Skip to content

Commit 3616cfd

Browse files
committed
solve 6 tests
1 parent 183b4bc commit 3616cfd

File tree

6 files changed

+28
-33
lines changed

6 files changed

+28
-33
lines changed

exercises/smart_pointers/arc1.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
// set of numbers within 8 different threads simultaneously. Each thread is
66
// going to get the sum of every eighth value, with an offset.
77
//
8+
// 本练习中,我们有一个名为 "numbers" 的 u32 向量,包含从 0 到 99 的值 -- [ 0, 1, 2, ..., 98, 99 ]。
9+
// 我们希望在 8 个不同的线程中同时使用这组数字。每个线程将计算每隔 8 个数的和,带有一个偏移量。
10+
//
811
// The first thread (offset 0), will sum 0, 8, 16, ...
12+
// 第一个线程(偏移量 0),会累加 0, 8, 16, ...
913
// The second thread (offset 1), will sum 1, 9, 17, ...
1014
// The third thread (offset 2), will sum 2, 10, 18, ...
1115
// ...
@@ -21,19 +25,18 @@
2125
//
2226
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
2327

24-
// I AM NOT DONE
2528

2629
#![forbid(unused_imports)] // Do not change this, (or the next) line.
2730
use std::sync::Arc;
2831
use std::thread;
2932

3033
fn main() {
3134
let numbers: Vec<_> = (0..100u32).collect();
32-
let shared_numbers = // TODO
35+
let shared_numbers = Arc::new(numbers);// TODO
3336
let mut joinhandles = Vec::new();
3437

3538
for offset in 0..8 {
36-
let child_numbers = // TODO
39+
let child_numbers = shared_numbers.clone(); // TODO
3740
joinhandles.push(thread::spawn(move || {
3841
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
3942
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(1, 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 & 11 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)]
@@ -59,18 +57,15 @@ fn main() {
5957
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
6058
jupiter.details();
6159

62-
// TODO
63-
let saturn = Planet::Saturn(Rc::new(Sun {}));
60+
let saturn = Planet::Saturn(Rc::clone(&sun));
6461
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
6562
saturn.details();
6663

67-
// TODO
68-
let uranus = Planet::Uranus(Rc::new(Sun {}));
64+
let uranus = Planet::Uranus(Rc::clone(&sun));
6965
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
7066
uranus.details();
7167

72-
// TODO
73-
let neptune = Planet::Neptune(Rc::new(Sun {}));
68+
let neptune = Planet::Neptune(Rc::clone(&sun));
7469
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
7570
neptune.details();
7671

@@ -91,13 +86,13 @@ fn main() {
9186
drop(mars);
9287
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
9388

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

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

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

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

exercises/threads/threads1.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
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;
14-
use std::time::{Duration, Instant};
12+
use std::time::{Duration, Instant, SystemTime};
1513

1614
fn main() {
1715
let mut handles = vec![];
@@ -27,6 +25,7 @@ fn main() {
2725
let mut results: Vec<u128> = vec![];
2826
for handle in handles {
2927
// TODO: a struct is returned from thread::spawn, can you use it?
28+
results.push(handle.join().unwrap());
3029
}
3130

3231
if results.len() != 10 {

exercises/threads/threads2.rs

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

10-
// I AM NOT DONE
11-
1210
use std::sync::Arc;
1311
use std::thread;
1412
use std::time::Duration;
13+
use std::sync::Mutex;
1514

1615
struct JobStatus {
1716
jobs_completed: u32,
1817
}
1918

2019
fn main() {
21-
let status = Arc::new(JobStatus { jobs_completed: 0 });
20+
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
2221
let mut handles = vec![];
2322
for _ in 0..10 {
24-
let status_shared = Arc::clone(&status);
23+
let status_shared = status.clone();
2524
let handle = thread::spawn(move || {
2625
thread::sleep(Duration::from_millis(250));
2726
// TODO: You must take an action before you update a shared value
28-
status_shared.jobs_completed += 1;
27+
status_shared.lock().unwrap().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
}

0 commit comments

Comments
 (0)