Rust变量绑定-范围
变量绑定有一个范围,并且被约束在一个块中。块是语句的集合括在大括号 {} 内,所以, 变量阴影 是被允许的。
fn main() { // 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); // This binding *shadows* the outer one let long_lived_binding = 5_f32; println!("inner long: {}", long_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); }
本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:代码驿站 [http:/www.codeinn.net]
本文标题:Rust变量绑定-范围
本文地址:http://www.codeinn.net/rust/1290.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:代码驿站 [http:/www.codeinn.net]
本文标题:Rust变量绑定-范围
本文地址:http://www.codeinn.net/rust/1290.html