summaryrefslogtreecommitdiff
path: root/rustlings/exercises/01_variables
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises/01_variables')
-rw-r--r--rustlings/exercises/01_variables/README.md9
-rw-r--r--rustlings/exercises/01_variables/variables1.rs6
-rw-r--r--rustlings/exercises/01_variables/variables2.rs10
-rw-r--r--rustlings/exercises/01_variables/variables3.rs6
-rw-r--r--rustlings/exercises/01_variables/variables4.rs8
-rw-r--r--rustlings/exercises/01_variables/variables5.rs8
-rw-r--r--rustlings/exercises/01_variables/variables6.rs6
7 files changed, 53 insertions, 0 deletions
diff --git a/rustlings/exercises/01_variables/README.md b/rustlings/exercises/01_variables/README.md
new file mode 100644
index 0000000..5ba2efc
--- /dev/null
+++ b/rustlings/exercises/01_variables/README.md
@@ -0,0 +1,9 @@
+# Variables
+
+In Rust, variables are immutable by default.
+When a variable is immutable, once a value is bound to a name, you can't change that value.
+You can make them mutable by adding `mut` in front of the variable name.
+
+## Further information
+
+- [Variables and Mutability](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html)
diff --git a/rustlings/exercises/01_variables/variables1.rs b/rustlings/exercises/01_variables/variables1.rs
new file mode 100644
index 0000000..f83b44d
--- /dev/null
+++ b/rustlings/exercises/01_variables/variables1.rs
@@ -0,0 +1,6 @@
+fn main() {
+ // TODO: Add the missing keyword.
+ x = 5;
+
+ println!("x has the value {x}");
+}
diff --git a/rustlings/exercises/01_variables/variables2.rs b/rustlings/exercises/01_variables/variables2.rs
new file mode 100644
index 0000000..e2a3603
--- /dev/null
+++ b/rustlings/exercises/01_variables/variables2.rs
@@ -0,0 +1,10 @@
+fn main() {
+ // TODO: Change the line below to fix the compiler error.
+ let x;
+
+ if x == 10 {
+ println!("x is ten!");
+ } else {
+ println!("x is not ten!");
+ }
+}
diff --git a/rustlings/exercises/01_variables/variables3.rs b/rustlings/exercises/01_variables/variables3.rs
new file mode 100644
index 0000000..06f35bb
--- /dev/null
+++ b/rustlings/exercises/01_variables/variables3.rs
@@ -0,0 +1,6 @@
+fn main() {
+ // TODO: Change the line below to fix the compiler error.
+ let x: i32;
+
+ println!("Number {x}");
+}
diff --git a/rustlings/exercises/01_variables/variables4.rs b/rustlings/exercises/01_variables/variables4.rs
new file mode 100644
index 0000000..6c138b1
--- /dev/null
+++ b/rustlings/exercises/01_variables/variables4.rs
@@ -0,0 +1,8 @@
+// TODO: Fix the compiler error.
+fn main() {
+ let x = 3;
+ println!("Number {x}");
+
+ x = 5; // Don't change this line
+ println!("Number {x}");
+}
diff --git a/rustlings/exercises/01_variables/variables5.rs b/rustlings/exercises/01_variables/variables5.rs
new file mode 100644
index 0000000..cf5620d
--- /dev/null
+++ b/rustlings/exercises/01_variables/variables5.rs
@@ -0,0 +1,8 @@
+fn main() {
+ 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 variable.
+ number = 3;
+ println!("Number plus two is: {}", number + 2);
+}
diff --git a/rustlings/exercises/01_variables/variables6.rs b/rustlings/exercises/01_variables/variables6.rs
new file mode 100644
index 0000000..4a040fd
--- /dev/null
+++ b/rustlings/exercises/01_variables/variables6.rs
@@ -0,0 +1,6 @@
+// TODO: Change the line below to fix the compiler error.
+const NUMBER = 3;
+
+fn main() {
+ println!("Number: {NUMBER}");
+}