Going through rustlings for the first time

ex(01): variables

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