summaryrefslogtreecommitdiff
path: root/rust/rustlings/solutions/01_variables
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
commit4dfc11d74ab02ac5765ae5d08fb750c91f4744b4 (patch)
tree17ba62056d6b691dd0217b0e8f1b3a5826dce0f6 /rust/rustlings/solutions/01_variables
parent02d8f1c62896576e1266553a56980eddbc2a88d7 (diff)
rustlings: move to rust/
Diffstat (limited to 'rust/rustlings/solutions/01_variables')
-rw-r--r--rust/rustlings/solutions/01_variables/variables1.rs6
-rw-r--r--rust/rustlings/solutions/01_variables/variables2.rs16
-rw-r--r--rust/rustlings/solutions/01_variables/variables3.rs15
-rw-r--r--rust/rustlings/solutions/01_variables/variables4.rs9
-rw-r--r--rust/rustlings/solutions/01_variables/variables5.rs9
-rw-r--r--rust/rustlings/solutions/01_variables/variables6.rs6
6 files changed, 61 insertions, 0 deletions
diff --git a/rust/rustlings/solutions/01_variables/variables1.rs b/rust/rustlings/solutions/01_variables/variables1.rs
new file mode 100644
index 0000000..58d046b
--- /dev/null
+++ b/rust/rustlings/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/rust/rustlings/solutions/01_variables/variables2.rs b/rust/rustlings/solutions/01_variables/variables2.rs
new file mode 100644
index 0000000..50b8d1b
--- /dev/null
+++ b/rust/rustlings/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/rust/rustlings/solutions/01_variables/variables3.rs b/rust/rustlings/solutions/01_variables/variables3.rs
new file mode 100644
index 0000000..15f6557
--- /dev/null
+++ b/rust/rustlings/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/rust/rustlings/solutions/01_variables/variables4.rs b/rust/rustlings/solutions/01_variables/variables4.rs
new file mode 100644
index 0000000..7de6bcb
--- /dev/null
+++ b/rust/rustlings/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/rust/rustlings/solutions/01_variables/variables5.rs b/rust/rustlings/solutions/01_variables/variables5.rs
new file mode 100644
index 0000000..0ea3903
--- /dev/null
+++ b/rust/rustlings/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/rust/rustlings/solutions/01_variables/variables6.rs b/rust/rustlings/solutions/01_variables/variables6.rs
new file mode 100644
index 0000000..25b7a1e
--- /dev/null
+++ b/rust/rustlings/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}");
+}