+8
-2
.rustlings-state.txt
+8
-2
.rustlings-state.txt
+1
-1
exercises/01_variables/variables1.rs
+1
-1
exercises/01_variables/variables1.rs
+1
-1
exercises/01_variables/variables2.rs
+1
-1
exercises/01_variables/variables2.rs
+1
-1
exercises/01_variables/variables3.rs
+1
-1
exercises/01_variables/variables3.rs
+1
-1
exercises/01_variables/variables4.rs
+1
-1
exercises/01_variables/variables4.rs
+1
-1
exercises/01_variables/variables5.rs
+1
-1
exercises/01_variables/variables5.rs
+1
-1
exercises/01_variables/variables6.rs
+1
-1
exercises/01_variables/variables6.rs
+4
-2
solutions/01_variables/variables1.rs
+4
-2
solutions/01_variables/variables1.rs
+14
-2
solutions/01_variables/variables2.rs
+14
-2
solutions/01_variables/variables2.rs
···
1
1
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
2
+
// The easiest way to fix the compiler error is to initialize the
3
+
// variable `x`. By setting its value to an integer, Rust infers its type
4
+
// as `i32` which is the default type for integers.
5
+
let x = 42;
6
+
7
+
// But we can enforce a type different from the default `i32` by adding
8
+
// a type annotation:
9
+
// let x: u8 = 42;
10
+
11
+
if x == 10 {
12
+
println!("x is ten!");
13
+
} else {
14
+
println!("x is not ten!");
15
+
}
4
16
}
+13
-2
solutions/01_variables/variables3.rs
+13
-2
solutions/01_variables/variables3.rs
···
1
+
#![allow(clippy::needless_late_init)]
2
+
1
3
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
4
+
// Reading uninitialized variables isn't allowed in Rust!
5
+
// Therefore, we need to assign a value first.
6
+
let x: i32 = 42;
7
+
8
+
println!("Number {x}");
9
+
10
+
// It is possible to declare a variable and initialize it later.
11
+
// But it can't be used before initialization.
12
+
let y: i32;
13
+
y = 42;
14
+
println!("Number {y}");
4
15
}
+7
-2
solutions/01_variables/variables4.rs
+7
-2
solutions/01_variables/variables4.rs
···
1
1
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
2
+
// In Rust, variables are immutable by default.
3
+
// Adding the `mut` keyword after `let` makes the declared variable mutable.
4
+
let mut x = 3;
5
+
println!("Number {x}");
6
+
7
+
x = 5;
8
+
println!("Number {x}");
4
9
}
+7
-2
solutions/01_variables/variables5.rs
+7
-2
solutions/01_variables/variables5.rs
···
1
1
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
2
+
let number = "T-H-R-E-E";
3
+
println!("Spell a number: {}", number);
4
+
5
+
// Using variable shadowing
6
+
// https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
7
+
let number = 3;
8
+
println!("Number plus two is: {}", number + 2);
4
9
}