From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Variable scope

Variable scope - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Variable scope

- [Presenter] It's time to shift our focus from learning about basic programming constructs like functions and loops to discuss some of the key concepts you'll need to consider when programming in Rust. Our first concept is scope which describes the region of a program or a specific variable is valid and can be used. You'll hear the term coming into scope to describe when the variable becomes valid and it will remain valid and available to use until it goes out of scope. In Rust, a variable binding is constrained to live within the block of code where it was created which is a section of code enclosed by a pair of curly braces. We created new blocks of code every time we write a function, a loop or a conditional expression and you can even use a pair of curly braces on their own to define a block of code with its own scope. Consider the scope of the planet variable initialized here. It lives between the curly braces of the…

Contents