···11fn main() {
22 // TODO: Add the missing keyword.
33- x = 5;
33+ let x = 5;
4455 println!("x has the value {x}");
66}
+1-1
exercises/01_variables/variables2.rs
···11fn main() {
22 // TODO: Change the line below to fix the compiler error.
33- let x;
33+ let x = 0;
4455 if x == 10 {
66 println!("x is ten!");
+1-1
exercises/01_variables/variables3.rs
···11fn main() {
22 // TODO: Change the line below to fix the compiler error.
33- let x: i32;
33+ let x: i32 = 0;
4455 println!("Number {x}");
66}
+1-1
exercises/01_variables/variables4.rs
···11// TODO: Fix the compiler error.
22fn main() {
33- let x = 3;
33+ let mut x = 3;
44 println!("Number {x}");
5566 x = 5; // Don't change this line
+1-1
exercises/01_variables/variables5.rs
···33 println!("Spell a number: {}", number);
4455 // TODO: Fix the compiler error by changing the line below without renaming the variable.
66- number = 3;
66+ let number = 3;
77 println!("Number plus two is: {}", number + 2);
88}
+1-1
exercises/01_variables/variables6.rs
···11// TODO: Change the line below to fix the compiler error.
22-const NUMBER = 3;
22+const NUMBER: i32 = 3;
3344fn main() {
55 println!("Number: {NUMBER}");
+4-2
solutions/01_variables/variables1.rs
···11fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
22+ // Declaring variables requires the `let` keyword.
33+ let x = 5;
44+55+ println!("x has the value {x}");
46}
+14-2
solutions/01_variables/variables2.rs
···11fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
22+ // The easiest way to fix the compiler error is to initialize the
33+ // variable `x`. By setting its value to an integer, Rust infers its type
44+ // as `i32` which is the default type for integers.
55+ let x = 42;
66+77+ // But we can enforce a type different from the default `i32` by adding
88+ // a type annotation:
99+ // let x: u8 = 42;
1010+1111+ if x == 10 {
1212+ println!("x is ten!");
1313+ } else {
1414+ println!("x is not ten!");
1515+ }
416}
+13-2
solutions/01_variables/variables3.rs
···11+#![allow(clippy::needless_late_init)]
22+13fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
44+ // Reading uninitialized variables isn't allowed in Rust!
55+ // Therefore, we need to assign a value first.
66+ let x: i32 = 42;
77+88+ println!("Number {x}");
99+1010+ // It is possible to declare a variable and initialize it later.
1111+ // But it can't be used before initialization.
1212+ let y: i32;
1313+ y = 42;
1414+ println!("Number {y}");
415}
+7-2
solutions/01_variables/variables4.rs
···11fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
22+ // In Rust, variables are immutable by default.
33+ // Adding the `mut` keyword after `let` makes the declared variable mutable.
44+ let mut x = 3;
55+ println!("Number {x}");
66+77+ x = 5;
88+ println!("Number {x}");
49}
+7-2
solutions/01_variables/variables5.rs
···11fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
22+ let number = "T-H-R-E-E";
33+ println!("Spell a number: {}", number);
44+55+ // Using variable shadowing
66+ // https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
77+ let number = 3;
88+ println!("Number plus two is: {}", number + 2);
49}
+4-2
solutions/01_variables/variables6.rs
···11+// The type of constants must always be annotated.
22+const NUMBER: u64 = 3;
33+14fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
55+ println!("Number: {NUMBER}");
46}