File tree 4 files changed +20
-19
lines changed
4 files changed +20
-19
lines changed Original file line number Diff line number Diff line change 21
21
//
22
22
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
23
23
24
- // I AM NOT DONE
25
24
26
25
#![ forbid( unused_imports) ] // Do not change this, (or the next) line.
27
26
use std:: sync:: Arc ;
28
27
use std:: thread;
29
28
30
29
fn main ( ) {
31
30
let numbers: Vec < _ > = ( 0 ..100u32 ) . collect ( ) ;
32
- let shared_numbers = // TODO
33
- let mut joinhandles = Vec :: new ( ) ;
31
+ let shared_numbers = Arc :: new ( numbers ) ; // TODO
32
+ let mut joinhandles = vec ! [ ] ;
34
33
35
34
for offset in 0 ..8 {
36
- let child_numbers = // TODO
35
+ let child_numbers = Arc :: clone ( & shared_numbers ) ; // TODO
37
36
joinhandles. push ( thread:: spawn ( move || {
38
37
let sum: u32 = child_numbers. iter ( ) . filter ( |& & n| n % 8 == offset) . sum ( ) ;
39
38
println ! ( "Sum of offset {} is {}" , offset, sum) ;
Original file line number Diff line number Diff line change 18
18
//
19
19
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
20
20
21
- // I AM NOT DONE
22
-
23
21
#[ derive( PartialEq , Debug ) ]
24
22
pub enum List {
25
- Cons ( i32 , List ) ,
23
+ Cons ( i32 , Box < List > ) ,
26
24
Nil ,
27
25
}
28
26
@@ -33,13 +31,14 @@ fn main() {
33
31
create_non_empty_list( )
34
32
) ;
35
33
}
36
-
34
+ use crate :: List :: Cons ;
35
+ use crate :: List :: Nil ;
37
36
pub fn create_empty_list ( ) -> List {
38
- todo ! ( )
37
+ Nil
39
38
}
40
39
41
40
pub fn create_non_empty_list ( ) -> List {
42
- todo ! ( )
41
+ Cons ( 1 , Box :: new ( Cons ( 2 , Box :: new ( Nil ) ) ) )
43
42
}
44
43
45
44
#[ cfg( test) ]
Original file line number Diff line number Diff line change 12
12
//
13
13
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
14
14
15
- // I AM NOT DONE
16
15
17
16
use std:: borrow:: Cow ;
18
17
@@ -48,7 +47,8 @@ mod tests {
48
47
let slice = [ 0 , 1 , 2 ] ;
49
48
let mut input = Cow :: from ( & slice[ ..] ) ;
50
49
match abs_all ( & mut input) {
51
- // TODO
50
+ Cow :: Borrowed ( _) => Ok ( ( ) ) ,
51
+ _ => Err ( "Expected borrowed value" ) ,
52
52
}
53
53
}
54
54
@@ -60,7 +60,8 @@ mod tests {
60
60
let slice = vec ! [ 0 , 1 , 2 ] ;
61
61
let mut input = Cow :: from ( slice) ;
62
62
match abs_all ( & mut input) {
63
- // TODO
63
+ Cow :: Owned ( _) => Ok ( ( ) ) ,
64
+ _ => Err ( "Expected borrowed or mutated value" ) ,
64
65
}
65
66
}
66
67
@@ -72,7 +73,8 @@ mod tests {
72
73
let slice = vec ! [ -1 , 0 , 1 ] ;
73
74
let mut input = Cow :: from ( slice) ;
74
75
match abs_all ( & mut input) {
75
- // TODO
76
+ Cow :: Owned ( _) => Ok ( ( ) ) ,
77
+ _ => Err ( "Expected mutated value" ) ,
76
78
}
77
79
}
78
80
}
Original file line number Diff line number Diff line change 10
10
//
11
11
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
12
12
13
- // I AM NOT DONE
14
-
15
13
use std:: rc:: Rc ;
16
14
17
15
#[ derive( Debug ) ]
@@ -60,17 +58,17 @@ fn main() {
60
58
jupiter. details ( ) ;
61
59
62
60
// TODO
63
- let saturn = Planet :: Saturn ( Rc :: new ( Sun { } ) ) ;
61
+ let saturn = Planet :: Saturn ( Rc :: clone ( & sun ) ) ;
64
62
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 7 references
65
63
saturn. details ( ) ;
66
64
67
65
// TODO
68
- let uranus = Planet :: Uranus ( Rc :: new ( Sun { } ) ) ;
66
+ let uranus = Planet :: Uranus ( Rc :: clone ( & sun ) ) ;
69
67
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 8 references
70
68
uranus. details ( ) ;
71
69
72
70
// TODO
73
- let neptune = Planet :: Neptune ( Rc :: new ( Sun { } ) ) ;
71
+ let neptune = Planet :: Neptune ( Rc :: clone ( & sun ) ) ;
74
72
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 9 references
75
73
neptune. details ( ) ;
76
74
@@ -92,12 +90,15 @@ fn main() {
92
90
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 4 references
93
91
94
92
// TODO
93
+ drop ( earth) ;
95
94
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 3 references
96
95
97
96
// TODO
97
+ drop ( venus) ;
98
98
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 2 references
99
99
100
100
// TODO
101
+ drop ( mercury) ;
101
102
println ! ( "reference count = {}" , Rc :: strong_count( & sun) ) ; // 1 reference
102
103
103
104
assert_eq ! ( Rc :: strong_count( & sun) , 1 ) ;
You can’t perform that action at this time.
0 commit comments