···1fn main() {
2 // TODO: Add the missing keyword.
3- x = 5;
45 println!("x has the value {x}");
6}
···1fn main() {
2 // TODO: Add the missing keyword.
3+ let x = 5;
45 println!("x has the value {x}");
6}
+1-1
exercises/01_variables/variables2.rs
···1fn main() {
2 // TODO: Change the line below to fix the compiler error.
3- let x;
45 if x == 10 {
6 println!("x is ten!");
···1fn main() {
2 // TODO: Change the line below to fix the compiler error.
3+ let x = 0;
45 if x == 10 {
6 println!("x is ten!");
+1-1
exercises/01_variables/variables3.rs
···1fn main() {
2 // TODO: Change the line below to fix the compiler error.
3- let x: i32;
45 println!("Number {x}");
6}
···1fn main() {
2 // TODO: Change the line below to fix the compiler error.
3+ let x: i32 = 0;
45 println!("Number {x}");
6}
+1-1
exercises/01_variables/variables4.rs
···1// TODO: Fix the compiler error.
2fn main() {
3- let x = 3;
4 println!("Number {x}");
56 x = 5; // Don't change this line
···1// TODO: Fix the compiler error.
2fn main() {
3+ let mut x = 3;
4 println!("Number {x}");
56 x = 5; // Don't change this line
+1-1
exercises/01_variables/variables5.rs
···3 println!("Spell a number: {}", number);
45 // TODO: Fix the compiler error by changing the line below without renaming the variable.
6- number = 3;
7 println!("Number plus two is: {}", number + 2);
8}
···3 println!("Spell a number: {}", number);
45 // TODO: Fix the compiler error by changing the line below without renaming the variable.
6+ let number = 3;
7 println!("Number plus two is: {}", number + 2);
8}
+1-1
exercises/01_variables/variables6.rs
···1// TODO: Change the line below to fix the compiler error.
2-const NUMBER = 3;
34fn main() {
5 println!("Number: {NUMBER}");
···1// TODO: Change the line below to fix the compiler error.
2+const NUMBER: i32 = 3;
34fn main() {
5 println!("Number: {NUMBER}");
+4-2
solutions/01_variables/variables1.rs
···1fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
004}
···1fn main() {
2+ // Declaring variables requires the `let` keyword.
3+ let x = 5;
4+5+ println!("x has the value {x}");
6}
+14-2
solutions/01_variables/variables2.rs
···1fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
0000000000004}
···1fn main() {
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+ }
16}
+13-2
solutions/01_variables/variables3.rs
···001fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
0000000004}
···1+#![allow(clippy::needless_late_init)]
2+3fn main() {
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}");
15}
+7-2
solutions/01_variables/variables4.rs
···1fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
000004}
···1fn main() {
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}");
9}
+7-2
solutions/01_variables/variables5.rs
···1fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
000004}
···1fn main() {
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);
9}
+4-2
solutions/01_variables/variables6.rs
···0001fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
4}
···1+// The type of constants must always be annotated.
2+const NUMBER: u64 = 3;
3+4fn main() {
5+ println!("Number: {NUMBER}");
06}