mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 10:02:42 +00:00
47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
// ./src/variable_bindings/scope.md
|
|
|
|
|
|
fn part0() {
|
|
// This binding lives in the main function
|
|
let long_lived_binding = 1;
|
|
|
|
// This is a block, and has a smaller scope than the main function
|
|
{
|
|
// This binding only exists in this block
|
|
let short_lived_binding = 2;
|
|
|
|
println!("inner short: {}", short_lived_binding);
|
|
}
|
|
// End of the block
|
|
|
|
// Error! `short_lived_binding` doesn't exist in this scope
|
|
// println!("outer short: {}", short_lived_binding);
|
|
// FIXME ^ Comment out this line
|
|
|
|
println!("outer long: {}", long_lived_binding);
|
|
}
|
|
|
|
fn part1() {
|
|
let shadowed_binding = 1;
|
|
|
|
{
|
|
println!("before being shadowed: {}", shadowed_binding);
|
|
|
|
// This binding *shadows* the outer one
|
|
let shadowed_binding = "abc";
|
|
|
|
println!("shadowed in inner block: {}", shadowed_binding);
|
|
}
|
|
println!("outside inner block: {}", shadowed_binding);
|
|
|
|
// This binding *shadows* the previous binding
|
|
let shadowed_binding = 2;
|
|
println!("shadowed in outer block: {}", shadowed_binding);
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
part1();
|
|
}
|
|
|