File tree Expand file tree Collapse file tree 5 files changed +20
-17
lines changed Expand file tree Collapse file tree 5 files changed +20
-17
lines changed Original file line number Diff line number Diff line change 1111// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
1212// hint.
1313
14- // I AM NOT DONE
1514use std:: collections:: HashMap ;
1615
1716#[ derive( Clone , Copy , PartialEq , Eq ) ]
Original file line number Diff line number Diff line change 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.
2725use std:: sync:: Arc ;
2826use std:: thread;
2927
3028fn 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) ;
Original file line number Diff line number Diff line change 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 ) ]
2422pub enum List {
25- Cons ( i32 , List ) ,
23+ Cons ( i32 , Box < List > ) ,
2624 Nil ,
2725}
2826
@@ -35,11 +33,14 @@ fn main() {
3533}
3634
3735pub fn create_empty_list ( ) -> List {
38- todo ! ( )
36+ List :: Nil
3937}
4038
4139pub 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) ]
Original file line number Diff line number Diff line change 1212//
1313// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
1414
15- // I AM NOT DONE
16-
1715use std:: borrow:: Cow ;
1816
1917fn 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}
Original file line number Diff line number Diff line change 1010//
1111// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
1212
13- // I AM NOT DONE
14-
1513use 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 ) ;
You can’t perform that action at this time.
0 commit comments