Skip to content

Commit

Permalink
Add variables example
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysemmas committed Oct 29, 2022
1 parent 3bbd719 commit 18b8c5d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions variables/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions variables/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "variables"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
25 changes: 25 additions & 0 deletions variables/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;

fn main() {
// Mutable variable can have value changed at will
let mut x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");

let y = 5;
// Can shadow and reuse the variable `y` by using `let` again
let y = y + 1;
{
// New `y` variable exists for life of the scope
let y = y * 2;
println!("The value of y in the inner scope is: {y}");
}

// `y` variable returns to value outside of the scope (6)
println!("The value of y is: {y}");

// We can use shadowing to change the variable's type
let spaces = " ";
let spaces = spaces.len();
}

0 comments on commit 18b8c5d

Please sign in to comment.