diff options
| author | Hunter Kvalevog <hunter@kvog.sh> | 2026-09-14 17:35:40 -0500 |
|---|---|---|
| committer | Hunter Kvalevog <hunter@kvog.sh> | 2026-09-14 17:35:40 -0500 |
| commit | f2eb3fbb17b8ed0b29b1ff1acab7ce4fccb64e23 (patch) | |
| tree | 44f363449ca4cfe66f2151fb581963ce4f9ae88c | |
| parent | e3694fe0c20e4b3fc6449d7581b2e60f6f3eb19e (diff) | |
rustlings: complete 00 and 01
| -rw-r--r-- | rustlings/.rustlings-state.txt | 12 | ||||
| -rw-r--r-- | rustlings/exercises/00_intro/intro1.rs | 4 | ||||
| -rw-r--r-- | rustlings/exercises/00_intro/intro2.rs | 3 | ||||
| -rw-r--r-- | rustlings/exercises/01_variables/variables1.rs | 3 | ||||
| -rw-r--r-- | rustlings/exercises/01_variables/variables2.rs | 3 | ||||
| -rw-r--r-- | rustlings/exercises/01_variables/variables3.rs | 3 | ||||
| -rw-r--r-- | rustlings/exercises/01_variables/variables4.rs | 3 | ||||
| -rw-r--r-- | rustlings/exercises/01_variables/variables5.rs | 3 | ||||
| -rw-r--r-- | rustlings/exercises/01_variables/variables6.rs | 3 | ||||
| -rw-r--r-- | rustlings/solutions/00_intro/intro1.rs | 5 | ||||
| -rw-r--r-- | rustlings/solutions/00_intro/intro2.rs | 4 | ||||
| -rw-r--r-- | rustlings/solutions/01_variables/variables1.rs | 6 | ||||
| -rw-r--r-- | rustlings/solutions/01_variables/variables2.rs | 16 | ||||
| -rw-r--r-- | rustlings/solutions/01_variables/variables3.rs | 15 | ||||
| -rw-r--r-- | rustlings/solutions/01_variables/variables4.rs | 9 | ||||
| -rw-r--r-- | rustlings/solutions/01_variables/variables5.rs | 9 | ||||
| -rw-r--r-- | rustlings/solutions/01_variables/variables6.rs | 6 |
17 files changed, 77 insertions, 30 deletions
diff --git a/rustlings/.rustlings-state.txt b/rustlings/.rustlings-state.txt new file mode 100644 index 0000000..b83f6a3 --- /dev/null +++ b/rustlings/.rustlings-state.txt @@ -0,0 +1,12 @@ +DON'T EDIT THIS FILE! + +functions1 + +intro1 +intro2 +variables1 +variables2 +variables3 +variables4 +variables5 +variables6 diff --git a/rustlings/exercises/00_intro/intro1.rs b/rustlings/exercises/00_intro/intro1.rs index 172a6ab..440ae83 100644 --- a/rustlings/exercises/00_intro/intro1.rs +++ b/rustlings/exercises/00_intro/intro1.rs @@ -21,4 +21,8 @@ fn main() { println!("The file of this exercise is `exercises/00_intro/intro1.rs`. Have a look!"); println!("The current exercise path will be always shown under the progress bar."); println!("You can click on the path to open the exercise file in your editor."); + println!(); + println!("New line"); + println!( " another line "); + println!(r#" yet another line"#); } diff --git a/rustlings/exercises/00_intro/intro2.rs b/rustlings/exercises/00_intro/intro2.rs index c6cb645..e0b5e27 100644 --- a/rustlings/exercises/00_intro/intro2.rs +++ b/rustlings/exercises/00_intro/intro2.rs @@ -1,4 +1,3 @@ fn main() { - // TODO: Fix the code to print "Hello world!". - printline!("Hello world!"); + println!("Hello world!"); } diff --git a/rustlings/exercises/01_variables/variables1.rs b/rustlings/exercises/01_variables/variables1.rs index f83b44d..2df7df8 100644 --- a/rustlings/exercises/01_variables/variables1.rs +++ b/rustlings/exercises/01_variables/variables1.rs @@ -1,6 +1,5 @@ fn main() { - // TODO: Add the missing keyword. - x = 5; + let x = 5; println!("x has the value {x}"); } diff --git a/rustlings/exercises/01_variables/variables2.rs b/rustlings/exercises/01_variables/variables2.rs index e2a3603..b35a0bf 100644 --- a/rustlings/exercises/01_variables/variables2.rs +++ b/rustlings/exercises/01_variables/variables2.rs @@ -1,6 +1,5 @@ fn main() { - // TODO: Change the line below to fix the compiler error. - let x; + let x = 10; if x == 10 { println!("x is ten!"); diff --git a/rustlings/exercises/01_variables/variables3.rs b/rustlings/exercises/01_variables/variables3.rs index 06f35bb..47b2be4 100644 --- a/rustlings/exercises/01_variables/variables3.rs +++ b/rustlings/exercises/01_variables/variables3.rs @@ -1,6 +1,5 @@ fn main() { - // TODO: Change the line below to fix the compiler error. - let x: i32; + let x = 10; println!("Number {x}"); } diff --git a/rustlings/exercises/01_variables/variables4.rs b/rustlings/exercises/01_variables/variables4.rs index 6c138b1..d2967ea 100644 --- a/rustlings/exercises/01_variables/variables4.rs +++ b/rustlings/exercises/01_variables/variables4.rs @@ -1,6 +1,5 @@ -// TODO: Fix the compiler error. fn main() { - let x = 3; + let mut x = 3; println!("Number {x}"); x = 5; // Don't change this line diff --git a/rustlings/exercises/01_variables/variables5.rs b/rustlings/exercises/01_variables/variables5.rs index cf5620d..be94598 100644 --- a/rustlings/exercises/01_variables/variables5.rs +++ b/rustlings/exercises/01_variables/variables5.rs @@ -2,7 +2,6 @@ fn main() { let number = "T-H-R-E-E"; // Don't change this line println!("Spell a number: {number}"); - // TODO: Fix the compiler error by changing the line below without renaming the variable. - number = 3; + let number = 3; println!("Number plus two is: {}", number + 2); } diff --git a/rustlings/exercises/01_variables/variables6.rs b/rustlings/exercises/01_variables/variables6.rs index 4a040fd..c40e572 100644 --- a/rustlings/exercises/01_variables/variables6.rs +++ b/rustlings/exercises/01_variables/variables6.rs @@ -1,5 +1,4 @@ -// TODO: Change the line below to fix the compiler error. -const NUMBER = 3; +const NUMBER: i32 = 3; fn main() { println!("Number: {NUMBER}"); diff --git a/rustlings/solutions/00_intro/intro1.rs b/rustlings/solutions/00_intro/intro1.rs index dcf2377..4fe8454 100644 --- a/rustlings/solutions/00_intro/intro1.rs +++ b/rustlings/solutions/00_intro/intro1.rs @@ -1,4 +1,5 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // Congratulations, you finished the first exercise 🎉 + // As an introduction to Rustlings, the first exercise only required + // entering `n` in the terminal to go to the next exercise. } diff --git a/rustlings/solutions/00_intro/intro2.rs b/rustlings/solutions/00_intro/intro2.rs index dcf2377..b8e031a 100644 --- a/rustlings/solutions/00_intro/intro2.rs +++ b/rustlings/solutions/00_intro/intro2.rs @@ -1,4 +1,4 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // `println!` instead of `printline!`. + println!("Hello world!"); } diff --git a/rustlings/solutions/01_variables/variables1.rs b/rustlings/solutions/01_variables/variables1.rs index dcf2377..58d046b 100644 --- a/rustlings/solutions/01_variables/variables1.rs +++ b/rustlings/solutions/01_variables/variables1.rs @@ -1,4 +1,6 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // Declaring variables requires the `let` keyword. + let x = 5; + + println!("x has the value {x}"); } diff --git a/rustlings/solutions/01_variables/variables2.rs b/rustlings/solutions/01_variables/variables2.rs index dcf2377..50b8d1b 100644 --- a/rustlings/solutions/01_variables/variables2.rs +++ b/rustlings/solutions/01_variables/variables2.rs @@ -1,4 +1,16 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // The easiest way to fix the compiler error is to initialize the + // variable `x`. By setting its value to an integer, Rust infers its type + // as `i32` which is the default type for integers. + let x = 42; + + // But we can enforce a type different from the default `i32` by adding + // a type annotation: + // let x: u8 = 42; + + if x == 10 { + println!("x is ten!"); + } else { + println!("x is not ten!"); + } } diff --git a/rustlings/solutions/01_variables/variables3.rs b/rustlings/solutions/01_variables/variables3.rs index dcf2377..15f6557 100644 --- a/rustlings/solutions/01_variables/variables3.rs +++ b/rustlings/solutions/01_variables/variables3.rs @@ -1,4 +1,15 @@ +#![allow(clippy::needless_late_init)] + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // Reading uninitialized variables isn't allowed in Rust! + // Therefore, we need to assign a value first. + let x: i32 = 42; + + println!("Number {x}"); + + // It is possible to declare a variable and initialize it later. + // But it can't be used before initialization. + let y: i32; + y = 42; + println!("Number {y}"); } diff --git a/rustlings/solutions/01_variables/variables4.rs b/rustlings/solutions/01_variables/variables4.rs index dcf2377..7de6bcb 100644 --- a/rustlings/solutions/01_variables/variables4.rs +++ b/rustlings/solutions/01_variables/variables4.rs @@ -1,4 +1,9 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // In Rust, variables are immutable by default. + // Adding the `mut` keyword after `let` makes the declared variable mutable. + let mut x = 3; + println!("Number {x}"); + + x = 5; + println!("Number {x}"); } diff --git a/rustlings/solutions/01_variables/variables5.rs b/rustlings/solutions/01_variables/variables5.rs index dcf2377..0ea3903 100644 --- a/rustlings/solutions/01_variables/variables5.rs +++ b/rustlings/solutions/01_variables/variables5.rs @@ -1,4 +1,9 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let number = "T-H-R-E-E"; + println!("Spell a number: {number}"); + + // Using variable shadowing + // https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing + let number = 3; + println!("Number plus two is: {}", number + 2); } diff --git a/rustlings/solutions/01_variables/variables6.rs b/rustlings/solutions/01_variables/variables6.rs index dcf2377..25b7a1e 100644 --- a/rustlings/solutions/01_variables/variables6.rs +++ b/rustlings/solutions/01_variables/variables6.rs @@ -1,4 +1,6 @@ +// The type of constants must always be annotated. +const NUMBER: u64 = 3; + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + println!("Number: {NUMBER}"); } |