diff options
Diffstat (limited to 'rust/rustlings/exercises/01_variables')
7 files changed, 47 insertions, 0 deletions
diff --git a/rust/rustlings/exercises/01_variables/README.md b/rust/rustlings/exercises/01_variables/README.md new file mode 100644 index 0000000..5ba2efc --- /dev/null +++ b/rust/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/rust/rustlings/exercises/01_variables/variables1.rs b/rust/rustlings/exercises/01_variables/variables1.rs new file mode 100644 index 0000000..2df7df8 --- /dev/null +++ b/rust/rustlings/exercises/01_variables/variables1.rs @@ -0,0 +1,5 @@ +fn main() { + let x = 5; + + println!("x has the value {x}"); +} diff --git a/rust/rustlings/exercises/01_variables/variables2.rs b/rust/rustlings/exercises/01_variables/variables2.rs new file mode 100644 index 0000000..b35a0bf --- /dev/null +++ b/rust/rustlings/exercises/01_variables/variables2.rs @@ -0,0 +1,9 @@ +fn main() { + let x = 10; + + if x == 10 { + println!("x is ten!"); + } else { + println!("x is not ten!"); + } +} diff --git a/rust/rustlings/exercises/01_variables/variables3.rs b/rust/rustlings/exercises/01_variables/variables3.rs new file mode 100644 index 0000000..47b2be4 --- /dev/null +++ b/rust/rustlings/exercises/01_variables/variables3.rs @@ -0,0 +1,5 @@ +fn main() { + let x = 10; + + println!("Number {x}"); +} diff --git a/rust/rustlings/exercises/01_variables/variables4.rs b/rust/rustlings/exercises/01_variables/variables4.rs new file mode 100644 index 0000000..d2967ea --- /dev/null +++ b/rust/rustlings/exercises/01_variables/variables4.rs @@ -0,0 +1,7 @@ +fn main() { + let mut x = 3; + println!("Number {x}"); + + x = 5; // Don't change this line + println!("Number {x}"); +} diff --git a/rust/rustlings/exercises/01_variables/variables5.rs b/rust/rustlings/exercises/01_variables/variables5.rs new file mode 100644 index 0000000..be94598 --- /dev/null +++ b/rust/rustlings/exercises/01_variables/variables5.rs @@ -0,0 +1,7 @@ +fn main() { + let number = "T-H-R-E-E"; // Don't change this line + println!("Spell a number: {number}"); + + let number = 3; + println!("Number plus two is: {}", number + 2); +} diff --git a/rust/rustlings/exercises/01_variables/variables6.rs b/rust/rustlings/exercises/01_variables/variables6.rs new file mode 100644 index 0000000..c40e572 --- /dev/null +++ b/rust/rustlings/exercises/01_variables/variables6.rs @@ -0,0 +1,5 @@ +const NUMBER: i32 = 3; + +fn main() { + println!("Number: {NUMBER}"); +} |