Skip to content

Instantly share code, notes, and snippets.

@georgematos
Last active May 2, 2024 14:25
Show Gist options
  • Save georgematos/1288a2fc53b4081d7a3af36cc5c09ea8 to your computer and use it in GitHub Desktop.
Save georgematos/1288a2fc53b4081d7a3af36cc5c09ea8 to your computer and use it in GitHub Desktop.
Rust - Shadowing sample
// shadowing sample
fn main() {
let x = 5;
// shadowing x variable
let x = x + 1; // at this point the x was overrided with a new value
println!("The value of x: {x}");
{
// current shadowing only lives within that scope
let x = x * 2;
println!("The value of x: {x}");
}
// the scoped shadowing dont influeces the outside x variable
println!("The value of x: {x}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment