mirror of
https://bitbucket.org/Mattrixwv/rusttutorials.git
synced 2025-12-07 11:03:58 -05:00
Added functions for chapter 4
This commit is contained in:
29
src/VariableBindings/ScopeAndShadowing.rs
Normal file
29
src/VariableBindings/ScopeAndShadowing.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
pub 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);
|
||||
|
||||
// This binding also *shadows* the previous binding
|
||||
let long_lived_binding = 'a';
|
||||
|
||||
println!("outer long: {}", long_lived_binding);
|
||||
}
|
||||
Reference in New Issue
Block a user