Skip to content

Commit a7dc798

Browse files
committed
chore(rustlings): revise some rustlings exercises
1 parent fdba9cf commit a7dc798

20 files changed

+41
-43
lines changed

Other exercises/rustlings/01_variables/variables2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
fn main() {
8-
let x = 2;
8+
let x = 1;
99
if x == 10 {
1010
println!("x is ten!");
1111
} else {

Other exercises/rustlings/01_variables/variables5.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
fn main() {
88
let number = "T-H-R-E-E"; // don't change this line
99
println!("Spell a Number : {}", number);
10-
{
11-
let number = 3; // don't rename this variable
12-
println!("Number plus two is : {}", number + 2);
13-
}
14-
10+
let number: i32 = 3; // don't rename this variable
11+
println!("Number plus two is : {}", number + 2);
1512
}

Other exercises/rustlings/02_functions/functions1.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
// hint.
55

66

7-
fn call_me() {
8-
9-
}
10-
117
fn main() {
128
call_me();
139
}
10+
11+
fn call_me() {
12+
println!("Call me maybe?");
13+
}

Other exercises/rustlings/02_functions/functions3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
fn main() {
8-
call_me(3);
8+
call_me(2);
99
}
1010

1111
fn call_me(num: u32) {

Other exercises/rustlings/02_functions/functions4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
println!("Your sale price is {}", sale_price(original_price));
1515
}
1616

17-
fn sale_price(price: i32) -> i32 {
17+
fn sale_price(price: i32) -> i32{
1818
if is_even(price) {
1919
price - 10
2020
} else {

Other exercises/rustlings/03_if/if1.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
pub fn bigger(a: i32, b: i32) -> i32 {
77
// Complete this function to return the bigger number!
8+
// If both numbers are equal, any of them can be returned.
89
// Do not use:
910
// - another function call
1011
// - additional variables
11-
if a <= b { return b; } else { return a; }
12+
if b > a {b} else {a}
1213
}
1314

1415
// Don't mind this for now :)

Other exercises/rustlings/03_if/if2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn foo_if_fizz(fizzish: &str) -> &str {
1111
"foo"
1212
} else if fizzish == "fuzz" {
1313
"bar"
14-
} else { return "baz" }
14+
} else {"baz"}
1515
}
1616

1717
// No test changes needed!

Other exercises/rustlings/03_if/if3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn animal_habitat(animal: &str) -> &'static str {
1111
} else if animal == "snake" {
1212
3
1313
} else {
14-
0
14+
2222
1515
};
1616

1717
// DO NOT CHANGE THIS STATEMENT BELOW

Other exercises/rustlings/04_primitive_types/primitive_types2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() {
1818
println!("Neither alphabetic nor numeric!");
1919
}
2020

21-
let your_character = 'Z'; // Finish this line like the example! What's your favorite character?
21+
let your_character = 'M';// Finish this line like the example! What's your favorite character?
2222
// Try a letter, try a number, try a special character, try a character
2323
// from a different language than your own, try an emoji!
2424
if your_character.is_alphabetic() {

Other exercises/rustlings/04_primitive_types/primitive_types3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
fn main() {
10-
let mut a = [999; 150];
10+
let a = ["hello"; 120];
1111

1212
if a.len() >= 100 {
1313
println!("Wow, that's a big array!");

Other exercises/rustlings/05_vecs/vecs1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
99

1010

11-
1211
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
1312
let a = [10, 20, 30, 40]; // a plain array
1413
let v = vec![10,20,30,40]; // TODO: declare your vector here with the macro for vectors

Other exercises/rustlings/05_vecs/vecs2.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
1212
for element in v.iter_mut() {
13+
// TODO: Fill this up so that each element in the Vec `v` is
14+
// multiplied by 2.
1315
*element *= 2
1416
}
1517

@@ -19,7 +21,9 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
1921

2022
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
2123
v.iter().map(|element| {
22-
element * 2
24+
// TODO: Do the same thing as above - but instead of mutating the
25+
// Vec, you can just return the new number!
26+
*element * 2
2327
}).collect()
2428
}
2529

Other exercises/rustlings/06_move_semantics/move_semantics2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
fn main() {
1111
let vec0 = vec![22, 44, 66];
1212

13-
let mut vec1 = fill_vec(vec0.clone());
13+
let vec1 = fill_vec(vec0.clone());
1414

1515
assert_eq!(vec0, vec![22, 44, 66]);
1616
assert_eq!(vec1, vec![22, 44, 66, 88]);

Other exercises/rustlings/06_move_semantics/move_semantics3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
fn main() {
1212
let vec0 = vec![22, 44, 66];
1313

14-
let mut vec1 = fill_vec(vec0);
14+
let vec1 = fill_vec(vec0);
1515

1616
assert_eq!(vec1, vec![22, 44, 66, 88]);
1717
}

Other exercises/rustlings/06_move_semantics/move_semantics4.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
fn main() {
1313
let vec0 = vec![22, 44, 66];
1414

15-
let mut vec1 = fill_vec();
15+
let vec1 = fill_vec();
1616

1717
assert_eq!(vec1, vec![22, 44, 66, 88]);
1818
}
1919

2020
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
2121
fn fill_vec() -> Vec<i32> {
2222
// Instead, let's create and fill the Vec in here - how do you do that?
23-
let mut vec = vec![];
24-
vec.push(22);
25-
vec.push(44);
26-
vec.push(66);
23+
let mut vec = vec![22,44,66];
24+
2725
vec.push(88);
2826

2927
vec

Other exercises/rustlings/06_move_semantics/move_semantics6.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ fn get_char(data: &String) -> char {
2323
fn string_uppercase(mut data: String) {
2424
data = data.to_uppercase();
2525

26-
println!("{}", &data);
26+
println!("{}", data);
2727
}

Other exercises/rustlings/07_structs/structs1.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,26 @@
77

88

99
struct ColorClassicStruct {
10-
red: i32,
11-
green: i32,
12-
blue: i32,
10+
red: u8,
11+
green: u8,
12+
blue: u8
1313
}
1414

15-
16-
struct ColorTupleStruct(
17-
i32,i32,i32 // can't give 'em names
18-
);
15+
struct ColorTupleStruct(u8,u8,u8);
1916

2017
#[derive(Debug)]
2118
struct UnitLikeStruct;
2219

23-
24-
2520
#[cfg(test)]
2621
mod tests {
2722
use super::*;
2823

2924
#[test]
3025
fn classic_c_structs() {
3126
let green = ColorClassicStruct {
32-
red: 0,green: 255,blue: 0
27+
red: 0,
28+
green: 255,
29+
blue: 0
3330
};
3431

3532
assert_eq!(green.red, 0);
@@ -40,15 +37,16 @@ mod tests {
4037
#[test]
4138
fn tuple_structs() {
4239
let green = ColorTupleStruct(0,255,0);
43-
40+
4441
assert_eq!(green.0, 0);
4542
assert_eq!(green.1, 255);
4643
assert_eq!(green.2, 0);
4744
}
4845

4946
#[test]
5047
fn unit_structs() {
51-
let message = format!("{:?}s are fun!", UnitLikeStruct);
48+
let unit_like_struct = UnitLikeStruct;
49+
let message = format!("{:?}s are fun!", unit_like_struct);
5250

5351
assert_eq!(message, "UnitLikeStructs are fun!");
5452
}

Other exercises/rustlings/07_structs/structs2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod tests {
3636
#[test]
3737
fn your_order() {
3838
let order_template = create_order_template();
39+
// TODO: Create your own order using the update syntax and template above!
3940
let your_order = Order {
4041
name: "Hacker in Rust".to_string(),
4142
count: 1,

Other exercises/rustlings/07_structs/structs3.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ impl Package {
3131
}
3232

3333
fn is_international(&self) -> bool {
34-
!(self.sender_country == self.recipient_country)
34+
self.recipient_country != self.sender_country
3535
}
3636

3737
fn get_fees(&self, cents_per_gram: u32) -> u32 {
38-
// Something goes here...
39-
self.weight_in_grams * cents_per_gram
38+
cents_per_gram * self.weight_in_grams
4039
}
4140
}
4241

Other exercises/rustlings/08_enums/enums1.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
#[derive(Debug)]
77
enum Message {
8+
// TODO: define a few types of messages as used below
89
Quit,
910
Echo,
1011
Move,
11-
ChangeColor,
12+
ChangeColor
1213
}
1314

1415
fn main() {

0 commit comments

Comments
 (0)