RBE more variable bindings fun
This commit is contained in:
parent
d2563484f9
commit
6d6e600720
19
rustbyexample/variable_bindings/declare.rs
Normal file
19
rustbyexample/variable_bindings/declare.rs
Normal file
@ -0,0 +1,19 @@
|
||||
fn main() {
|
||||
let a_binding;
|
||||
|
||||
{
|
||||
let x = 2;
|
||||
|
||||
a_binding = x * x;
|
||||
}
|
||||
|
||||
println!("a binding: {}", a_binding);
|
||||
|
||||
let another_binding;
|
||||
|
||||
//println!("another binding: {}", another_binding);
|
||||
|
||||
another_binding = 1;
|
||||
|
||||
println!("another binding: {}", another_binding);
|
||||
}
|
11
rustbyexample/variable_bindings/freeze.rs
Normal file
11
rustbyexample/variable_bindings/freeze.rs
Normal file
@ -0,0 +1,11 @@
|
||||
fn main() {
|
||||
let mut _mutable_integer = 7i32;
|
||||
|
||||
{
|
||||
let _mutable_integer = _mutable_integer;
|
||||
|
||||
// _mutable_integer = 50;
|
||||
}
|
||||
|
||||
_mutable_integer = 3;
|
||||
}
|
12
rustbyexample/variable_bindings/scope.rs
Normal file
12
rustbyexample/variable_bindings/scope.rs
Normal file
@ -0,0 +1,12 @@
|
||||
fn main() {
|
||||
let long_lived_binding = 1;
|
||||
|
||||
{
|
||||
let short_lived_binding = 2;
|
||||
println!("inner short: {}", short_lived_binding);
|
||||
}
|
||||
|
||||
// println!("outer short: {}", short_lived_binding);
|
||||
|
||||
println!("outer long: {}", long_lived_binding);
|
||||
}
|
16
rustbyexample/variable_bindings/scope2.rs
Normal file
16
rustbyexample/variable_bindings/scope2.rs
Normal file
@ -0,0 +1,16 @@
|
||||
fn main() {
|
||||
let shadowed_binding = 1;
|
||||
|
||||
{
|
||||
println!("before being shadowed: {}", shadowed_binding);
|
||||
|
||||
let shadowed_binding = "abc";
|
||||
|
||||
println!("shadowed in inner block: {}", shadowed_binding);
|
||||
}
|
||||
println!("outside inner block: {}", shadowed_binding);
|
||||
|
||||
let shadowed_binding = 2;
|
||||
println!("shadowed in outer block: {}", shadowed_binding);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user