November 2017
Intermediate to advanced
264 pages
5h 45m
English
All variables defined in the program bindings.rs have local scope delimited by the { } of the function which happens to be the main() function here, but this applies to any function. After the ending, }, they go out of scope and their memory allocation is freed.
We can even make a more limited scope inside a function by defining a code block as all code contained within a pair of curly braces { }, as in the following snippet:
// see Chapter 2/code/scope.rs fn main() { let outer = 42; { // start of code block let inner = 3.14; println!("block variable: {}", inner); let outer = 99; // shadows the first outer variable println!("block variable outer: {}", outer); } // end of code block println!("outer variable: ...Read now
Unlock full access