Created
November 26, 2023 18:51
-
-
Save i-asimkhan/265b568fe36e2eca4a43f6009df0bd15 to your computer and use it in GitHub Desktop.
Scope and Constants in #Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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