Skip to content

Instantly share code, notes, and snippets.

@i-asimkhan
Created November 26, 2023 18:51
Show Gist options
  • Save i-asimkhan/265b568fe36e2eca4a43f6009df0bd15 to your computer and use it in GitHub Desktop.
Save i-asimkhan/265b568fe36e2eca4a43f6009df0bd15 to your computer and use it in GitHub Desktop.
Scope and Constants in #Rust
#[allow(dead_code)]
#[allow(unused_variables)]
fn main() {
scope_and_shadowing();
constnts();
}
fn scope_and_shadowing() {
let age = 23;
{
let age = 34;
println!("Child age = {}", age);
}
println!("Parent age = {}", age);
}
const AVERAGE_AGE : u8 = 60; // no fixed address
static PREFIX : char = 'A'; // fixed address
static mut AGE : u8 = 22; // mutable fixed address
fn constnts() {
println!("Average age is {}", AVERAGE_AGE);
println!("Prefix character is {}", PREFIX);
unsafe {
println!("Age earlier was {}", AGE);
AGE = 59;
println!("Age now is {}", AGE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment