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() { fn main() {
printline!("Hello there!") printline!("Hello world!");
} }

View file

@ -1,6 +1,6 @@
// Make me compile!
fn main() { fn main() {
// TODO: Add missing keyword.
x = 5; x = 5;
println!("x has the value {}", x);
println!("x has the value {x}");
} }

View file

@ -1,5 +1,7 @@
fn main() { fn main() {
// TODO: Change the line below to fix the compiler error.
let x; let x;
if x == 10 { if x == 10 {
println!("x is ten!"); println!("x is ten!");
} else { } else {

View file

@ -1,4 +1,6 @@
fn main() { fn main() {
// TODO: Change the line below to fix the compiler error.
let x: i32; let x: i32;
println!("Number {}", x);
println!("Number {x}");
} }

View file

@ -1,6 +1,9 @@
// TODO: Fix the compiler error.
fn main() { fn main() {
let x = 3; let x = 3;
println!("Number {}", x); println!("Number {x}");
x = 5; // don't change this line
println!("Number {}", x); x = 5; // Don't change this line
println!("Number {x}");
} }

View file

@ -1,6 +1,8 @@
fn main() { fn main() {
let number = "T-H-R-E-E"; // don't change this line let number = "T-H-R-E-E"; // Don't change this line
println!("Spell a Number : {}", number); println!("Spell a number: {}", number);
number = 3; // don't rename this variable
// TODO: Fix the compiler error by changing the line below without renaming the the variable.
number = 3;
println!("Number plus two is: {}", number + 2); 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; const NUMBER = 3;
fn main() { 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 # INTRO
# TODO: Update exercise
[[exercises]] [[exercises]]
name = "intro1" name = "intro1"
dir = "00_intro" dir = "00_intro"
test = false test = false
# TODO: Fix hint hint = """
hint = """Enter `n` to move on to the next exercise. You might need to press ENTER after typing `n`.""" Enter `n` to move on to the next exercise.
You might need to press ENTER after typing `n`."""
[[exercises]] [[exercises]]
name = "intro2" name = "intro2"
dir = "00_intro" dir = "00_intro"
test = false test = false
hint = """ 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 # VARIABLES
@ -51,18 +52,18 @@ name = "variables1"
dir = "01_variables" dir = "01_variables"
test = false test = false
hint = """ hint = """
The declaration in the first line in the main function is missing a keyword The declaration in the `main` function is missing a keyword that is needed
that is needed in Rust to create a new variable binding.""" in Rust to create a new variable binding."""
[[exercises]] [[exercises]]
name = "variables2" name = "variables2"
dir = "01_variables" dir = "01_variables"
test = false test = false
hint = """ 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. 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? annotation?
What if you give `x` a value? What if you give `x` a value?
@ -78,9 +79,9 @@ name = "variables3"
dir = "01_variables" dir = "01_variables"
test = false test = false
hint = """ hint = """
Oops! In this exercise, we have a variable binding that we've created on in the In this exercise, we have a variable binding that we've created in the `main`
first line in the `main` function, and we're trying to use it in the next line, function, and we're trying to use it in the next line, but we haven't given it
but we haven't given it a value. a value.
We can't print out something that isn't there; try giving `x` 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" dir = "01_variables"
test = false test = false
hint = """ 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 to reassign a different value to `x`! There's a keyword we can use to make
a variable binding mutable instead.""" a variable binding mutable instead."""
@ -120,12 +121,12 @@ dir = "01_variables"
test = false test = false
hint = """ hint = """
We know about variables and mutability, but there is another important type of 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 Constants are always immutable. They are declared with the keyword `const` instead
than keyword `let`. 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 Read more about constants and the differences between variables and constants
under 'Constants' in the book's section 'Variables and Mutability': under 'Constants' in the book's section 'Variables and Mutability':
@ -139,7 +140,7 @@ name = "functions1"
dir = "02_functions" dir = "02_functions"
test = false test = false
hint = """ 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`. 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. It expects this function to not take any arguments and not return a value.
Sounds a lot like `main`, doesn't it?""" Sounds a lot like `main`, doesn't it?"""
@ -688,7 +689,7 @@ test = false
hint = """ hint = """
If other functions can return a `Result`, why shouldn't `main`? It's a fairly 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 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 The unit (`()`) type is there because nothing is really needed in terms of
positive results.""" 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}");
}