通过例子学习Rust

4.2 作用域和覆盖

变量具有局部作用域,被限制在其所属的代码块(block)内。(代码块指被花括号{}围住的语句组合。) Rust允许变量屏蔽(variable shadowing)

fn main() { // This variable lives in the main function let long_lived_variable = 1i; // This is a block, and has a smaller scope than the main function { // This variable only exists in this block let short_lived_variable = 2i; println!("inner short: {}", short_lived_variable); // This variable *shadows* the outer one let long_lived_variable = 5_f32; println!("inner long: {}", long_lived_variable); } // End of the block // Error! `short_lived_variable` doesn't exist in this scope println!("outer short: {}", short_lived_variable); // FIXME ^ Comment out this line println!("outer long: {}", long_lived_variable); }