summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-05-21 01:47:57 +0200
committermo8it <mo8it@proton.me>2024-05-21 01:47:57 +0200
commit0f4c42d54ea7322a4ee0ae7036c058c3061e80e9 (patch)
treedd72045b9c25da9da24e28842ef89058d83f8647
parentbde2524c3b1043da489541d4885592abe16646fa (diff)
Add solutions to intro and variables
-rw-r--r--exercises/00_intro/intro2.rs4
-rw-r--r--exercises/01_variables/variables1.rs6
-rw-r--r--exercises/01_variables/variables2.rs2
-rw-r--r--exercises/01_variables/variables3.rs4
-rw-r--r--exercises/01_variables/variables4.rs9
-rw-r--r--exercises/01_variables/variables5.rs10
-rw-r--r--exercises/01_variables/variables6.rs4
-rw-r--r--rustlings-macros/info.toml37
-rw-r--r--solutions/00_intro/intro1.rs3
-rw-r--r--solutions/00_intro/intro2.rs5
-rw-r--r--solutions/01_variables/variables1.rs7
-rw-r--r--solutions/01_variables/variables2.rs17
-rw-r--r--solutions/01_variables/variables3.rs14
-rw-r--r--solutions/01_variables/variables4.rs10
-rw-r--r--solutions/01_variables/variables5.rs10
-rw-r--r--solutions/01_variables/variables6.rs7
16 files changed, 109 insertions, 40 deletions
diff --git a/exercises/00_intro/intro2.rs b/exercises/00_intro/intro2.rs
index c7a3ab2..e443ec8 100644
--- a/exercises/00_intro/intro2.rs
+++ b/exercises/00_intro/intro2.rs
@@ -1,5 +1,5 @@
-// Make the code print a greeting to the world.
+// TODO: Fix the code to print "Hello world!".
fn main() {
- printline!("Hello there!")
+ printline!("Hello world!");
}
diff --git a/exercises/01_variables/variables1.rs b/exercises/01_variables/variables1.rs
index 3035bfa..0a9e554 100644
--- a/exercises/01_variables/variables1.rs
+++ b/exercises/01_variables/variables1.rs
@@ -1,6 +1,6 @@
-// Make me compile!
-
fn main() {
+ // TODO: Add missing keyword.
x = 5;
- println!("x has the value {}", x);
+
+ println!("x has the value {x}");
}
diff --git a/exercises/01_variables/variables2.rs b/exercises/01_variables/variables2.rs
index ce2dd85..e2a3603 100644
--- a/exercises/01_variables/variables2.rs
+++ b/exercises/01_variables/variables2.rs
@@ -1,5 +1,7 @@
fn main() {
+ // TODO: Change the line below to fix the compiler error.
let x;
+
if x == 10 {
println!("x is ten!");
} else {
diff --git a/exercises/01_variables/variables3.rs b/exercises/01_variables/variables3.rs
index 488385b..06f35bb 100644
--- a/exercises/01_variables/variables3.rs
+++ b/exercises/01_variables/variables3.rs
@@ -1,4 +1,6 @@
fn main() {
+ // TODO: Change the line below to fix the compiler error.
let x: i32;
- println!("Number {}", x);
+
+ println!("Number {x}");
}
diff --git a/exercises/01_variables/variables4.rs b/exercises/01_variables/variables4.rs
index 67be127..8634ceb 100644
--- a/exercises/01_variables/variables4.rs
+++ b/exercises/01_variables/variables4.rs
@@ -1,6 +1,9 @@
+// TODO: Fix the compiler error.
+
fn main() {
let x = 3;
- println!("Number {}", x);
- x = 5; // don't change this line
- println!("Number {}", x);
+ println!("Number {x}");
+
+ x = 5; // Don't change this line
+ println!("Number {x}");
}
diff --git a/exercises/01_variables/variables5.rs b/exercises/01_variables/variables5.rs
index 3a74541..73f655e 100644
--- a/exercises/01_variables/variables5.rs
+++ b/exercises/01_variables/variables5.rs
@@ -1,6 +1,8 @@
fn main() {
- let number = "T-H-R-E-E"; // don't change this line
- println!("Spell a Number : {}", number);
- number = 3; // don't rename this variable
- println!("Number plus two is : {}", number + 2);
+ 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 the variable.
+ number = 3;
+ println!("Number plus two is: {}", number + 2);
}
diff --git a/exercises/01_variables/variables6.rs b/exercises/01_variables/variables6.rs
index 4746331..4a040fd 100644
--- a/exercises/01_variables/variables6.rs
+++ b/exercises/01_variables/variables6.rs
@@ -1,4 +1,6 @@
+// TODO: Change the line below to fix the compiler error.
const NUMBER = 3;
+
fn main() {
- println!("Number {}", NUMBER);
+ println!("Number: {NUMBER}");
}
diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml
index 485665e..be3b262 100644
--- a/rustlings-macros/info.toml
+++ b/rustlings-macros/info.toml
@@ -29,20 +29,21 @@ https://github.com/rust-lang/rustlings/blob/main/CONTRIBUTING.md
# INTRO
-# TODO: Update exercise
[[exercises]]
name = "intro1"
dir = "00_intro"
test = false
-# TODO: Fix hint
-hint = """Enter `n` to move on to the next exercise. You might need to press ENTER after typing `n`."""
+hint = """
+Enter `n` to move on to the next exercise.
+You might need to press ENTER after typing `n`."""
[[exercises]]
name = "intro2"
dir = "00_intro"
test = false
hint = """
-The compiler is informing us that we've got the name of the print macro wrong, and has suggested an alternative."""
+The compiler is informing us that we've got the name of the print macro wrong.
+It also suggests an alternative."""
# VARIABLES
@@ -51,18 +52,18 @@ name = "variables1"
dir = "01_variables"
test = false
hint = """
-The declaration in the first line in the main function is missing a keyword
-that is needed in Rust to create a new variable binding."""
+The declaration in the `main` function is missing a keyword that is needed
+in Rust to create a new variable binding."""
[[exercises]]
name = "variables2"
dir = "01_variables"
test = false
hint = """
-The compiler message is saying that Rust cannot infer the type that the
+The compiler message is saying that Rust can't infer the type that the
variable binding `x` has with what is given here.
-What happens if you annotate the first line in the main function with a type
+What happens if you annotate the first line in the `main` function with a type
annotation?
What if you give `x` a value?
@@ -78,9 +79,9 @@ name = "variables3"
dir = "01_variables"
test = false
hint = """
-Oops! In this exercise, we have a variable binding that we've created on in the
-first line in the `main` function, and we're trying to use it in the next line,
-but we haven't given it a value.
+In this exercise, we have a variable binding that we've created in the `main`
+function, and we're trying to use it in the next line, but we haven't given it
+a value.
We can't print out something that isn't there; try giving `x` a value!
@@ -92,7 +93,7 @@ name = "variables4"
dir = "01_variables"
test = false
hint = """
-In Rust, variable bindings are immutable by default. But here we're trying
+In Rust, variable bindings are immutable by default. But here, we're trying
to reassign a different value to `x`! There's a keyword we can use to make
a variable binding mutable instead."""
@@ -120,12 +121,12 @@ dir = "01_variables"
test = false
hint = """
We know about variables and mutability, but there is another important type of
-variable available: constants.
+variables available: constants.
-Constants are always immutable and they are declared with keyword `const` rather
-than keyword `let`.
+Constants are always immutable. They are declared with the keyword `const` instead
+of `let`.
-Constants types must also always be annotated.
+The type of Constants must always be annotated.
Read more about constants and the differences between variables and constants
under 'Constants' in the book's section 'Variables and Mutability':
@@ -139,7 +140,7 @@ name = "functions1"
dir = "02_functions"
test = false
hint = """
-This main function is calling a function that it expects to exist, but the
+This `main` function is calling a function that it expects to exist, but the
function doesn't exist. It expects this function to have the name `call_me`.
It expects this function to not take any arguments and not return a value.
Sounds a lot like `main`, doesn't it?"""
@@ -688,7 +689,7 @@ test = false
hint = """
If other functions can return a `Result`, why shouldn't `main`? It's a fairly
common convention to return something like `Result<(), ErrorType>` from your
-main function.
+`main` function.
The unit (`()`) type is there because nothing is really needed in terms of
positive results."""
diff --git a/solutions/00_intro/intro1.rs b/solutions/00_intro/intro1.rs
index 4e18198..07d4e4f 100644
--- a/solutions/00_intro/intro1.rs
+++ b/solutions/00_intro/intro1.rs
@@ -1 +1,2 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+// The exercise `intro1` only requires entering `n` in the terminal to go to the next exercise.
+// It is just an introduction to how Rustlings works.
diff --git a/solutions/00_intro/intro2.rs b/solutions/00_intro/intro2.rs
index 4e18198..b8e031a 100644
--- a/solutions/00_intro/intro2.rs
+++ b/solutions/00_intro/intro2.rs
@@ -1 +1,4 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn main() {
+ // `println!` instead of `printline!`.
+ println!("Hello world!");
+}
diff --git a/solutions/01_variables/variables1.rs b/solutions/01_variables/variables1.rs
index 4e18198..58d046b 100644
--- a/solutions/01_variables/variables1.rs
+++ b/solutions/01_variables/variables1.rs
@@ -1 +1,6 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn main() {
+ // Declaring variables requires the `let` keyword.
+ let x = 5;
+
+ println!("x has the value {x}");
+}
diff --git a/solutions/01_variables/variables2.rs b/solutions/01_variables/variables2.rs
index 4e18198..50b8d1b 100644
--- a/solutions/01_variables/variables2.rs
+++ b/solutions/01_variables/variables2.rs
@@ -1 +1,16 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn main() {
+ // 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/solutions/01_variables/variables3.rs b/solutions/01_variables/variables3.rs
index 4e18198..7db42a9 100644
--- a/solutions/01_variables/variables3.rs
+++ b/solutions/01_variables/variables3.rs
@@ -1 +1,13 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn main() {
+ // Reading uninitialized variables isn't allowed in Rust!
+ // Therefore, we need to assign a value first.
+ let x: i32 = 42;
+
+ println!("Number {x}");
+
+ // It 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/solutions/01_variables/variables4.rs b/solutions/01_variables/variables4.rs
index 4e18198..0540caa 100644
--- a/solutions/01_variables/variables4.rs
+++ b/solutions/01_variables/variables4.rs
@@ -1 +1,9 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn main() {
+ // 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; // Don't change this line
+ println!("Number {x}");
+}
diff --git a/solutions/01_variables/variables5.rs b/solutions/01_variables/variables5.rs
index 4e18198..456dc9c 100644
--- a/solutions/01_variables/variables5.rs
+++ b/solutions/01_variables/variables5.rs
@@ -1 +1,9 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn main() {
+ let number = "T-H-R-E-E"; // Don't change this line
+ 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/solutions/01_variables/variables6.rs b/solutions/01_variables/variables6.rs
index 4e18198..25b7a1e 100644
--- a/solutions/01_variables/variables6.rs
+++ b/solutions/01_variables/variables6.rs
@@ -1 +1,6 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+// The type of constants must always be annotated.
+const NUMBER: u64 = 3;
+
+fn main() {
+ println!("Number: {NUMBER}");
+}