Add solutions to intro and variables

This commit is contained in:
mo8it 2024-05-21 01:47:57 +02:00
parent bde2524c3b
commit 0f4c42d54e
16 changed files with 109 additions and 40 deletions

View file

@ -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!");
}

View file

@ -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}");
}

View file

@ -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 {

View file

@ -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}");
}

View file

@ -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}");
}

View file

@ -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);
}

View file

@ -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}");
}

View file

@ -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."""

View file

@ -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.

View file

@ -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!");
}

View file

@ -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}");
}

View file

@ -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!");
}
}

View file

@ -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}");
}

View file

@ -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}");
}

View file

@ -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);
}

View file

@ -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}");
}