1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-11-29 22:47:43 +02:00

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