Skip to content

Commit 4186147

Browse files
yllmisyllmis
authored andcommitted
update
1 parent 82a67eb commit 4186147

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

exercises/iterators/iterators5.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
1212
// hint.
1313

14-
// I AM NOT DONE
1514
use std::collections::HashMap;
1615

1716
#[derive(Clone, Copy, PartialEq, Eq)]

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); // TODO
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); // TODO
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: 6 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,14 @@ 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(
41+
1,
42+
Box::new(List::Cons(2, Box::new(List::Cons(3, Box::new(List::Nil))))),
43+
)
4344
}
4445

4546
#[cfg(test)]

exercises/smart_pointers/cow1.rs

Lines changed: 6 additions & 2 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]> {
@@ -49,6 +47,8 @@ mod tests {
4947
let mut input = Cow::from(&slice[..]);
5048
match abs_all(&mut input) {
5149
// TODO
50+
Cow::Borrowed(_) => Ok(()),
51+
_ => Err("Expected borrowed value"),
5252
}
5353
}
5454

@@ -61,6 +61,8 @@ mod tests {
6161
let mut input = Cow::from(slice);
6262
match abs_all(&mut input) {
6363
// TODO
64+
Cow::Owned(_) => Ok(()),
65+
_ => Err("Expected owned value"),
6466
}
6567
}
6668

@@ -73,6 +75,8 @@ mod tests {
7375
let mut input = Cow::from(slice);
7476
match abs_all(&mut input) {
7577
// TODO
78+
Cow::Owned(_) => Ok(()),
79+
_ => Err("Expected owned value"),
7680
}
7781
}
7882
}

exercises/smart_pointers/rc1.rs

Lines changed: 6 additions & 5 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

@@ -92,12 +90,15 @@ fn main() {
9290
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
9391

9492
// TODO
93+
drop(earth);
9594
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
9695

9796
// TODO
97+
drop(venus);
9898
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
9999

100100
// TODO
101+
drop(mercury);
101102
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
102103

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

0 commit comments

Comments
 (0)