-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0001-Complelte-57-100.patch
1428 lines (1243 loc) · 42.9 KB
/
0001-Complelte-57-100.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From c557d85b246a36b32d5d716331031e800aa63190 Mon Sep 17 00:00:00 2001
From: "Guo, Jade" <[email protected]>
Date: Tue, 10 Oct 2023 11:51:26 +0800
Subject: [PATCH] Complelte 57/100
---
exercises/enums/enums1.rs | 6 +++--
exercises/enums/enums2.rs | 7 +++---
exercises/enums/enums3.rs | 12 ++++++++--
exercises/error_handling/errors1.rs | 7 +++---
exercises/error_handling/errors2.rs | 3 +--
exercises/error_handling/errors3.rs | 5 ++--
exercises/error_handling/errors4.rs | 10 ++++++--
exercises/error_handling/errors5.rs | 3 +--
exercises/error_handling/errors6.rs | 14 +++++++----
exercises/functions/functions1.rs | 5 +++-
exercises/functions/functions2.rs | 3 +--
exercises/functions/functions3.rs | 3 +--
exercises/functions/functions4.rs | 3 +--
exercises/functions/functions5.rs | 3 +--
exercises/hashmaps/hashmaps1.rs | 5 ++--
exercises/hashmaps/hashmaps2.rs | 6 ++---
exercises/hashmaps/hashmaps3.rs | 23 ++++++++++++++++++-
exercises/if/if1.rs | 10 ++++----
exercises/if/if2.rs | 5 ++--
exercises/if/if3.rs | 5 ++--
exercises/intro/intro2.rs | 3 +--
exercises/modules/modules1.rs | 3 +--
exercises/modules/modules2.rs | 9 ++++----
exercises/modules/modules3.rs | 3 +--
exercises/move_semantics/move_semantics1.rs | 9 +++-----
exercises/move_semantics/move_semantics2.rs | 4 +---
exercises/move_semantics/move_semantics3.rs | 2 +-
exercises/move_semantics/move_semantics4.rs | 8 ++-----
exercises/move_semantics/move_semantics5.rs | 3 +--
exercises/move_semantics/move_semantics6.rs | 22 ++++++++++++------
exercises/options/options1.rs | 9 +++++---
exercises/options/options2.rs | 8 +++----
exercises/options/options3.rs | 3 +--
exercises/primitive_types/primitive_types1.rs | 3 +--
exercises/primitive_types/primitive_types2.rs | 3 +--
exercises/primitive_types/primitive_types3.rs | 6 ++---
exercises/primitive_types/primitive_types4.rs | 3 +--
exercises/primitive_types/primitive_types5.rs | 3 +--
exercises/primitive_types/primitive_types6.rs | 4 +---
exercises/quiz1.rs | 10 +++++---
exercises/quiz2.rs | 21 +++++++++++++----
exercises/strings/strings1.rs | 4 +---
exercises/strings/strings2.rs | 3 +--
exercises/strings/strings3.rs | 8 +++----
exercises/strings/strings4.rs | 21 ++++++++---------
exercises/structs/structs1.rs | 18 +++++++--------
exercises/structs/structs2.rs | 7 ++++--
exercises/structs/structs3.rs | 10 ++++----
exercises/variables/variables1.rs | 3 +--
exercises/variables/variables2.rs | 4 +---
exercises/variables/variables3.rs | 3 +--
exercises/variables/variables4.rs | 3 +--
exercises/variables/variables5.rs | 3 +--
exercises/variables/variables6.rs | 3 +--
exercises/vecs/vecs1.rs | 4 +---
exercises/vecs/vecs2.rs | 5 ++--
56 files changed, 200 insertions(+), 176 deletions(-)
diff --git a/exercises/enums/enums1.rs b/exercises/enums/enums1.rs
index 25525b2..74967b6 100644
--- a/exercises/enums/enums1.rs
+++ b/exercises/enums/enums1.rs
@@ -2,11 +2,13 @@
//
// No hints this time! ;)
-// I AM NOT DONE
#[derive(Debug)]
enum Message {
- // TODO: define a few types of messages as used below
+ Quit,
+ Echo,
+ Move,
+ ChangeColor,
}
fn main() {
diff --git a/exercises/enums/enums2.rs b/exercises/enums/enums2.rs
index df93fe0..508d15b 100644
--- a/exercises/enums/enums2.rs
+++ b/exercises/enums/enums2.rs
@@ -3,11 +3,12 @@
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
-
#[derive(Debug)]
enum Message {
- // TODO: define the different variants used below
+ Move{x:i32,y:i32},
+ Echo(String),
+ ChangeColor(i32,i32,i32),
+ Quit,
}
impl Message {
diff --git a/exercises/enums/enums3.rs b/exercises/enums/enums3.rs
index 5d28441..ef17501 100644
--- a/exercises/enums/enums3.rs
+++ b/exercises/enums/enums3.rs
@@ -5,10 +5,12 @@
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
enum Message {
- // TODO: implement the message variant types based on their usage below
+ ChangeColor(u8,u8,u8),
+ Echo(String),
+ Move(Point),
+ Quit,
}
struct Point {
@@ -43,6 +45,12 @@ impl State {
// variants
// Remember: When passing a tuple as a function argument, you'll need
// extra parentheses: fn function((t, u, p, l, e))
+ match message{
+ Message::ChangeColor(r,g,b) => self.change_color((r,g,b)),
+ Message::Echo(s) => self.echo(s),
+ Message::Move(p) => self.move_position(p),
+ Message::Quit => self.quit(),
+ }
}
}
diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs
index 13d2724..7fc41aa 100644
--- a/exercises/error_handling/errors1.rs
+++ b/exercises/error_handling/errors1.rs
@@ -9,14 +9,13 @@
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
-pub fn generate_nametag_text(name: String) -> Option<String> {
+pub fn generate_nametag_text(name: String) -> Result<String,String> {
if name.is_empty() {
// Empty names aren't allowed.
- None
+ Err("`name` was empty; it must be nonempty.".into())
} else {
- Some(format!("Hi! My name is {}", name))
+ Ok(format!("Hi! My name is {}", name))
}
}
diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs
index d86f326..cabd047 100644
--- a/exercises/error_handling/errors2.rs
+++ b/exercises/error_handling/errors2.rs
@@ -19,14 +19,13 @@
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
use std::num::ParseIntError;
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
- let qty = item_quantity.parse::<i32>();
+ let qty = item_quantity.parse::<i32>()?;
Ok(qty * cost_per_item + processing_fee)
}
diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs
index d42d3b1..b29e084 100644
--- a/exercises/error_handling/errors3.rs
+++ b/exercises/error_handling/errors3.rs
@@ -7,11 +7,9 @@
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
-
use std::num::ParseIntError;
-fn main() {
+fn main()-> Result<(), ParseIntError> {
let mut tokens = 100;
let pretend_user_input = "8";
@@ -23,6 +21,7 @@ fn main() {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
+ Ok(())
}
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs
index e04bff7..329635e 100644
--- a/exercises/error_handling/errors4.rs
+++ b/exercises/error_handling/errors4.rs
@@ -3,7 +3,6 @@
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
@@ -17,7 +16,14 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm...? Why is this only returning an Ok value?
- Ok(PositiveNonzeroInteger(value as u64))
+ if value > 0 {
+ Ok(PositiveNonzeroInteger(value as u64))
+ } else if value == 0{
+ Err(CreationError::Zero)
+ } else {
+ Err(CreationError::Negative)
+ }
+
}
}
diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs
index 92461a7..3954dbe 100644
--- a/exercises/error_handling/errors5.rs
+++ b/exercises/error_handling/errors5.rs
@@ -22,14 +22,13 @@
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
use std::error;
use std::fmt;
use std::num::ParseIntError;
// TODO: update the return type of `main()` to make this compile.
-fn main() -> Result<(), Box<dyn ???>> {
+fn main() -> Result<(), Box<dyn error::Error>> {
let pretend_user_input = "42";
let x: i64 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs
index aaf0948..fc7bace 100644
--- a/exercises/error_handling/errors6.rs
+++ b/exercises/error_handling/errors6.rs
@@ -9,7 +9,6 @@
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
use std::num::ParseIntError;
@@ -25,14 +24,21 @@ impl ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
// TODO: add another error conversion function here.
- // fn from_parseint...
+ fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
+ ParsePosNonzeroError::ParseInt(err)
+ }
}
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
// TODO: change this to return an appropriate error instead of panicking
// when `parse()` returns an error.
- let x: i64 = s.parse().unwrap();
- PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
+ // let x: i64 = s.parse().unwrap().map_err(ParsePosNonzeroError::from_parseint);
+
+ match s.parse::<i64>() {
+ Err(e) => Err(e).map_err(ParsePosNonzeroError::from_parseint),
+ Ok(x) => PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation),
+ }
+
}
// Don't change anything below this line.
diff --git a/exercises/functions/functions1.rs b/exercises/functions/functions1.rs
index 40ed9a0..c1d9932 100644
--- a/exercises/functions/functions1.rs
+++ b/exercises/functions/functions1.rs
@@ -3,8 +3,11 @@
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
fn main() {
call_me();
}
+
+fn call_me(){
+ println!("hey!");
+}
diff --git a/exercises/functions/functions2.rs b/exercises/functions/functions2.rs
index 5154f34..70610cd 100644
--- a/exercises/functions/functions2.rs
+++ b/exercises/functions/functions2.rs
@@ -3,13 +3,12 @@
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
fn main() {
call_me(3);
}
-fn call_me(num:) {
+fn call_me(num: i32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
diff --git a/exercises/functions/functions3.rs b/exercises/functions/functions3.rs
index 74f44d6..49a520c 100644
--- a/exercises/functions/functions3.rs
+++ b/exercises/functions/functions3.rs
@@ -3,10 +3,9 @@
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
fn main() {
- call_me();
+ call_me(5);
}
fn call_me(num: u32) {
diff --git a/exercises/functions/functions4.rs b/exercises/functions/functions4.rs
index 77c4b2a..3b41a75 100644
--- a/exercises/functions/functions4.rs
+++ b/exercises/functions/functions4.rs
@@ -8,14 +8,13 @@
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}
-fn sale_price(price: i32) -> {
+fn sale_price(price: i32) -> i32{
if is_even(price) {
price - 10
} else {
diff --git a/exercises/functions/functions5.rs b/exercises/functions/functions5.rs
index f1b63f4..a9dd1b5 100644
--- a/exercises/functions/functions5.rs
+++ b/exercises/functions/functions5.rs
@@ -3,7 +3,6 @@
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
fn main() {
let answer = square(3);
@@ -11,5 +10,5 @@ fn main() {
}
fn square(num: i32) -> i32 {
- num * num;
+ num * num
}
diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs
index 80829ea..fcd8ead 100644
--- a/exercises/hashmaps/hashmaps1.rs
+++ b/exercises/hashmaps/hashmaps1.rs
@@ -11,15 +11,16 @@
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> {
- let mut basket = // TODO: declare your hash map here.
+ let mut basket = HashMap::new(); // TODO: declare your hash map here.
// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);
+ basket.insert(String::from("watermelon"), 5);
+ basket.insert(String::from("peach"), 20);
// TODO: Put more fruits in your basket here.
diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs
index a592569..924c22d 100644
--- a/exercises/hashmaps/hashmaps2.rs
+++ b/exercises/hashmaps/hashmaps2.rs
@@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
use std::collections::HashMap;
@@ -38,8 +37,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
for fruit in fruit_kinds {
// TODO: Insert new fruits if they are not already present in the
- // basket. Note that you are not allowed to put any type of fruit that's
- // already present!
+ basket.entry(fruit).or_insert(5);
}
}
@@ -81,7 +79,7 @@ mod tests {
let count = basket.values().sum::<u32>();
assert!(count > 11);
}
-
+
#[test]
fn all_fruit_types_in_basket() {
let mut basket = get_fruit_basket();
diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs
index 08e977c..4a59b8d 100644
--- a/exercises/hashmaps/hashmaps3.rs
+++ b/exercises/hashmaps/hashmaps3.rs
@@ -14,11 +14,11 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
use std::collections::HashMap;
// A structure to store the goal details of a team.
+#[derive(Debug)]
struct Team {
goals_scored: u8,
goals_conceded: u8,
@@ -39,6 +39,26 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded from team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by
// team_1.
+ println!("Jade: {} {} {} {} ", team_1_name, team_1_score, team_2_name, team_2_score);
+ if let Some(t) = scores.get_mut(&team_1_name) {
+ println!("team: {:?}",t);
+ t.goals_scored += team_1_score;
+ t.goals_conceded += team_2_score;
+ } else {
+ let team = Team{goals_scored:team_1_score, goals_conceded:team_2_score};
+ println!("new tean: {:?}", team);
+ scores.insert(team_1_name.clone(),team);
+ };
+
+ if let Some(t) = scores.get_mut(&team_2_name) {
+ println!("team: {:?}",t);
+ t.goals_scored += team_2_score;
+ t.goals_conceded += team_1_score;
+ } else {
+ let team = Team{goals_scored:team_2_score, goals_conceded:team_1_score};
+ println!("new tean: {:?}", team);
+ scores.insert(team_2_name.clone(),team);
+ };
}
scores
}
@@ -71,6 +91,7 @@ mod tests {
#[test]
fn validate_team_score_1() {
let scores = build_scores_table(get_results());
+ println!("{:?}", scores);
let team = scores.get("England").unwrap();
assert_eq!(team.goals_scored, 5);
assert_eq!(team.goals_conceded, 4);
diff --git a/exercises/if/if1.rs b/exercises/if/if1.rs
index d8108a0..5f68c0f 100644
--- a/exercises/if/if1.rs
+++ b/exercises/if/if1.rs
@@ -2,13 +2,13 @@
//
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
-
pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
- // Do not use:
- // - another function call
- // - additional variables
+ if a > b {
+ a
+ } else {
+ b
+ }
}
// Don't mind this for now :)
diff --git a/exercises/if/if2.rs b/exercises/if/if2.rs
index f512f13..40c0780 100644
--- a/exercises/if/if2.rs
+++ b/exercises/if/if2.rs
@@ -5,13 +5,14 @@
//
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
+ } else if fizzish == "fuzz" {
+ "bar"
} else {
- 1
+ "baz"
}
}
diff --git a/exercises/if/if3.rs b/exercises/if/if3.rs
index 73a7025..0f5db15 100644
--- a/exercises/if/if3.rs
+++ b/exercises/if/if3.rs
@@ -2,17 +2,16 @@
//
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
-// I AM NOT DONE
pub fn animal_habitat(animal: &str) -> &'static str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
- 2.0
+ 2
} else if animal == "snake" {
3
} else {
- "Unknown"
+ -1
};
// DO NOT CHANGE THIS STATEMENT BELOW
diff --git a/exercises/intro/intro2.rs b/exercises/intro/intro2.rs
index 990b20f..74aa3a2 100644
--- a/exercises/intro/intro2.rs
+++ b/exercises/intro/intro2.rs
@@ -5,8 +5,7 @@
// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
fn main() {
- println!("Hello {}!");
+ println!("Hello {}!", "World");
}
diff --git a/exercises/modules/modules1.rs b/exercises/modules/modules1.rs
index 9eb5a48..50b60bc 100644
--- a/exercises/modules/modules1.rs
+++ b/exercises/modules/modules1.rs
@@ -3,7 +3,6 @@
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
mod sausage_factory {
// Don't let anybody outside of this module see this!
@@ -11,7 +10,7 @@ mod sausage_factory {
String::from("Ginger")
}
- fn make_sausage() {
+ pub fn make_sausage() {
get_secret_recipe();
println!("sausage!");
}
diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs
index 0415454..eba71f7 100644
--- a/exercises/modules/modules2.rs
+++ b/exercises/modules/modules2.rs
@@ -7,19 +7,18 @@
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
mod delicious_snacks {
// TODO: Fix these use statements
- use self::fruits::PEAR as ???
- use self::veggies::CUCUMBER as ???
+ pub use self::fruits::PEAR as fruit;
+ pub use self::veggies::CUCUMBER as veggie;
- mod fruits {
+ pub mod fruits {
pub const PEAR: &'static str = "Pear";
pub const APPLE: &'static str = "Apple";
}
- mod veggies {
+ pub mod veggies {
pub const CUCUMBER: &'static str = "Cucumber";
pub const CARROT: &'static str = "Carrot";
}
diff --git a/exercises/modules/modules3.rs b/exercises/modules/modules3.rs
index f2bb050..30b6c13 100644
--- a/exercises/modules/modules3.rs
+++ b/exercises/modules/modules3.rs
@@ -8,10 +8,9 @@
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
// TODO: Complete this use statement
-use ???
+use std::time::{UNIX_EPOCH,SystemTime};
fn main() {
match SystemTime::now().duration_since(UNIX_EPOCH) {
diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs
index 710d20d..088ff1a 100644
--- a/exercises/move_semantics/move_semantics1.rs
+++ b/exercises/move_semantics/move_semantics1.rs
@@ -3,12 +3,11 @@
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
- let vec0 = Vec::new();
+ let mut vec0 = Vec::new();
- let vec1 = fill_vec(vec0);
+ let vec1 = fill_vec(&mut vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@@ -17,9 +16,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
-fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
- let mut vec = vec;
-
+fn fill_vec(vec: &mut Vec<i32>) -> &mut Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);
diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs
index 72d37fa..c7b0d9f 100644
--- a/exercises/move_semantics/move_semantics2.rs
+++ b/exercises/move_semantics/move_semantics2.rs
@@ -7,12 +7,10 @@
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
-
fn main() {
let vec0 = Vec::new();
- let mut vec1 = fill_vec(vec0);
+ let mut vec1 = fill_vec(vec0.clone());
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs
index ea21493..5f09afe 100644
--- a/exercises/move_semantics/move_semantics3.rs
+++ b/exercises/move_semantics/move_semantics3.rs
@@ -6,7 +6,6 @@
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
@@ -21,6 +20,7 @@ fn main() {
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
+ let mut vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);
diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs
index 75a3b6b..0f4a06b 100644
--- a/exercises/move_semantics/move_semantics4.rs
+++ b/exercises/move_semantics/move_semantics4.rs
@@ -7,12 +7,9 @@
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
- let vec0 = Vec::new();
-
- let mut vec1 = fill_vec(vec0);
+ let mut vec1 = fill_vec();
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@@ -23,8 +20,7 @@ fn main() {
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
- let mut vec = vec;
-
+ let mut vec = Vec::new();
vec.push(22);
vec.push(44);
vec.push(66);
diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs
index 68db09e..4d70062 100644
--- a/exercises/move_semantics/move_semantics5.rs
+++ b/exercises/move_semantics/move_semantics5.rs
@@ -6,13 +6,12 @@
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
let mut x = 100;
let y = &mut x;
- let z = &mut x;
*y += 100;
+ let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs
index cace4ca..8878baa 100644
--- a/exercises/move_semantics/move_semantics6.rs
+++ b/exercises/move_semantics/move_semantics6.rs
@@ -5,24 +5,32 @@
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
-
fn main() {
let data = "Rust is great!".to_string();
- get_char(data);
+ get_char(&data);
+
+ string_uppercase(data);
+
+ let num = 3;
+ add_one(num);
+ println!("{num}");
+}
- string_uppercase(&data);
+fn add_one(mut num:i32) -> i32{
+ num = num + 1;
+ println!("{num}");
+ num
}
// Should not take ownership
-fn get_char(data: String) -> char {
+fn get_char(data: &String) -> char {
data.chars().last().unwrap()
}
// Should take ownership
-fn string_uppercase(mut data: &String) {
- data = &data.to_uppercase();
+fn string_uppercase(mut data: String) {
+ data = data.to_uppercase();
println!("{}", data);
}
diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs
index e131b48..61ae676 100644
--- a/exercises/options/options1.rs
+++ b/exercises/options/options1.rs
@@ -3,7 +3,6 @@
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
// This function returns how much icecream there is left in the fridge.
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
@@ -13,7 +12,11 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// value of 0 The Option output should gracefully handle cases where
// time_of_day > 23.
// TODO: Complete the function body - remember to return an Option!
- ???
+ match time_of_day{
+ 0..=21 => Some(5),
+ 22..=24 => Some(0),
+ _ => None,
+ }
}
#[cfg(test)]
@@ -34,6 +37,6 @@ mod tests {
// TODO: Fix this test. How do you get at the value contained in the
// Option?
let icecreams = maybe_icecream(12);
- assert_eq!(icecreams, 5);
+ assert_eq!(icecreams.unwrap(), 5);
}
}
diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs
index 4d998e7..3d1802d 100644
--- a/exercises/options/options2.rs
+++ b/exercises/options/options2.rs
@@ -3,8 +3,6 @@
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
-
#[cfg(test)]
mod tests {
#[test]
@@ -13,9 +11,10 @@ mod tests {
let optional_target = Some(target);
// TODO: Make this an if let statement whose value is "Some" type
- word = optional_target {
+ if let Some(word) = optional_target {
assert_eq!(word, target);
}
+
}
#[test]
@@ -28,11 +27,10 @@ mod tests {
}
let mut cursor = range;
-
// TODO: make this a while let statement - remember that vector.pop also
// adds another layer of Option<T>. You can stack `Option<T>`s into
// while let and if let.
- integer = optional_integers.pop() {
+ while let Some(Some(integer)) = optional_integers.pop() {
assert_eq!(integer, cursor);
cursor -= 1;
}
diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs
index 23c15ea..76664ff 100644
--- a/exercises/options/options3.rs
+++ b/exercises/options/options3.rs
@@ -3,7 +3,6 @@
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
// hint.
-// I AM NOT DONE
struct Point {
x: i32,
@@ -14,7 +13,7 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y {
- Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
+ Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => panic!("no match!"),
}
y; // Fix without deleting this line.
diff --git a/exercises/primitive_types/primitive_types1.rs b/exercises/primitive_types/primitive_types1.rs
index e1cf52a..a7b5b3e 100644
--- a/exercises/primitive_types/primitive_types1.rs
+++ b/exercises/primitive_types/primitive_types1.rs
@@ -6,7 +6,6 @@
// Execute `rustlings hint primitive_types1` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
// Booleans (`bool`)
@@ -16,7 +15,7 @@ fn main() {
println!("Good morning!");
}
- let // Finish the rest of this line like the example! Or make it be false!
+ let is_evening = false; // Finish the rest of this line like the example! Or make it be false!
if is_evening {
println!("Good evening!");
}
diff --git a/exercises/primitive_types/primitive_types2.rs b/exercises/primitive_types/primitive_types2.rs
index fcc9705..a95d0fe 100644
--- a/exercises/primitive_types/primitive_types2.rs
+++ b/exercises/primitive_types/primitive_types2.rs
@@ -6,7 +6,6 @@
// Execute `rustlings hint primitive_types2` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
// Characters (`char`)
@@ -22,7 +21,7 @@ fn main() {
println!("Neither alphabetic nor numeric!");
}
- let // Finish this line like the example! What's your favorite character?
+ let your_character = '2'; // Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji!
if your_character.is_alphabetic() {
diff --git a/exercises/primitive_types/primitive_types3.rs b/exercises/primitive_types/primitive_types3.rs
index 06a7a62..934ef42 100644
--- a/exercises/primitive_types/primitive_types3.rs
+++ b/exercises/primitive_types/primitive_types3.rs
@@ -5,13 +5,11 @@
// Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
-
fn main() {
- let a = ???
+ let a = [1;200];
if a.len() >= 100 {
- println!("Wow, that's a big array!");
+ println!("Wow, that's a big array!:{:?}",a);
} else {
println!("Meh, I eat arrays like that for breakfast.");
}
diff --git a/exercises/primitive_types/primitive_types4.rs b/exercises/primitive_types/primitive_types4.rs
index d44d877..9564c7a 100644
--- a/exercises/primitive_types/primitive_types4.rs
+++ b/exercises/primitive_types/primitive_types4.rs
@@ -5,13 +5,12 @@
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];
- let nice_slice = ???
+ let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice)
}
diff --git a/exercises/primitive_types/primitive_types5.rs b/exercises/primitive_types/primitive_types5.rs
index f646986..6f547e0 100644
--- a/exercises/primitive_types/primitive_types5.rs
+++ b/exercises/primitive_types/primitive_types5.rs
@@ -5,11 +5,10 @@
// Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
fn main() {
let cat = ("Furry McFurson", 3.5);
- let /* your pattern here */ = cat;
+ let (name, age) /* your pattern here */ = cat;
println!("{} is {} years old.", name, age);
}
diff --git a/exercises/primitive_types/primitive_types6.rs b/exercises/primitive_types/primitive_types6.rs
index 07cc46c..0ed7cfd 100644
--- a/exercises/primitive_types/primitive_types6.rs
+++ b/exercises/primitive_types/primitive_types6.rs
@@ -6,13 +6,11 @@
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand
// for a hint.
-// I AM NOT DONE
-
#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax.
- let second = ???;
+ let second = numbers.1;
assert_eq!(2, second,
"This is not the 2nd number in the tuple!")