diff options
| author | mo8it <mo8it@proton.me> | 2024-07-05 13:39:50 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-07-05 13:39:50 +0200 |
| commit | 7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch) | |
| tree | c67f7e62bb9a179ae4fdbab492501cb6847e64c7 /solutions/01_variables | |
| parent | 77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff) | |
| parent | 4d9c346a173bb722b929f3ea3c00f84954483e24 (diff) | |
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'solutions/01_variables')
| -rw-r--r-- | solutions/01_variables/variables1.rs | 6 | ||||
| -rw-r--r-- | solutions/01_variables/variables2.rs | 16 | ||||
| -rw-r--r-- | solutions/01_variables/variables3.rs | 15 | ||||
| -rw-r--r-- | solutions/01_variables/variables4.rs | 9 | ||||
| -rw-r--r-- | solutions/01_variables/variables5.rs | 9 | ||||
| -rw-r--r-- | solutions/01_variables/variables6.rs | 6 |
6 files changed, 61 insertions, 0 deletions
diff --git a/solutions/01_variables/variables1.rs b/solutions/01_variables/variables1.rs new file mode 100644 index 0000000..58d046b --- /dev/null +++ b/solutions/01_variables/variables1.rs @@ -0,0 +1,6 @@ +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 new file mode 100644 index 0000000..50b8d1b --- /dev/null +++ b/solutions/01_variables/variables2.rs @@ -0,0 +1,16 @@ +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 new file mode 100644 index 0000000..15f6557 --- /dev/null +++ b/solutions/01_variables/variables3.rs @@ -0,0 +1,15 @@ +#![allow(clippy::needless_late_init)] + +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 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/solutions/01_variables/variables4.rs b/solutions/01_variables/variables4.rs new file mode 100644 index 0000000..7de6bcb --- /dev/null +++ b/solutions/01_variables/variables4.rs @@ -0,0 +1,9 @@ +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; + println!("Number {x}"); +} diff --git a/solutions/01_variables/variables5.rs b/solutions/01_variables/variables5.rs new file mode 100644 index 0000000..9057754 --- /dev/null +++ b/solutions/01_variables/variables5.rs @@ -0,0 +1,9 @@ +fn main() { + 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/solutions/01_variables/variables6.rs b/solutions/01_variables/variables6.rs new file mode 100644 index 0000000..25b7a1e --- /dev/null +++ b/solutions/01_variables/variables6.rs @@ -0,0 +1,6 @@ +// The type of constants must always be annotated. +const NUMBER: u64 = 3; + +fn main() { + println!("Number: {NUMBER}"); +} |
