summaryrefslogtreecommitdiff
path: root/rustlings/solutions
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/solutions')
-rw-r--r--rustlings/solutions/00_intro/intro1.rs5
-rw-r--r--rustlings/solutions/00_intro/intro2.rs4
-rw-r--r--rustlings/solutions/01_variables/variables1.rs6
-rw-r--r--rustlings/solutions/01_variables/variables2.rs16
-rw-r--r--rustlings/solutions/01_variables/variables3.rs15
-rw-r--r--rustlings/solutions/01_variables/variables4.rs9
-rw-r--r--rustlings/solutions/01_variables/variables5.rs9
-rw-r--r--rustlings/solutions/01_variables/variables6.rs6
8 files changed, 54 insertions, 16 deletions
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}");
}