Rust - Concept of Scope, Block, and Shadowing
Last Updated :
22 Aug, 2022
Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references.
In this article, we will discuss the concepts behind the scope, block, and shadowing in Rust programming.
Scope and Block of a Variable:
The scope of a variable in Rust refers to the part of the code where one can access the variable.
In Rust, curly braces { } define the block scope where the variable access becomes restricted to local. Any variable outside the braces would be having global access i.e. access from anywhere inside the function.
A collection of statements enclosed by braces { } are called blocks. To understand the concept of blocks, we need to understand variables.
Types of Variables:
- Local Variables: Local variables are variables that are defined inside a function scope. Their scope remains inside the curly braces i.e. its scope and not accessible outside.
- Global Variables: Global variables are variables that are declared outside the blocks and hence can be accessed inside any further blocks. They are accessible anywhere.
Example 1:
Rust
fn main() {
let global_variable = "GeeksforGeeks" ; //
{ // start of code block
let local_variable = "GFG"; //
println!("Inner variable: {}", local_variable);
println!("Outer variable: {}", global_variable);
} // end of code block
}
Output:
Shadowing:
In Rust, we come across a concept known as shadowing. Shadowing in a variable refers to a technique where a variable defined in a particular scope has the same name as a variable declared on another scope i.e. outer scope. This is referred to as masking. Therefore, the outer variable becomes shadowed by the inner variable, and the inner variable masks the outer variable.
Example 2:
Rust
fn main() {
let outer_variable = "GeeksforGeeks_1";
{ // start of code block
let inner_variable = "GFG";
println!("The inner block variable is: {}", inner_variable);
let outer_variable = "GeeksforGeeks_2";
println!("The outer block variable : {}", outer_variable);
} // end of code block
println!("The outer variable is: {}", outer_variable);
}
Output:
Here, outer_variable = "GeeksforGeeks_1" is the variable that is outside the block, and outer_variable =" GeeksforGeeks_2" is the variable that is inside the block.
Similar Reads
Rust - Concept of Structures Structures are basically user-defined type mainly used for grouping different data types together. It overcomes the limitation of a traditional array that can only contain elements of the same data type. A structure can also contain a structure in itself. In Rust, structures are similar to tuples, b
3 min read
Rust - Concept of Capturing Variables with Closers In Rust, we can capture variables via Closures. Closures are anonymous functions that help us in saving variables or pass arguments to other functions. Closures can be created from one place and then can be called anywhere and they can capture values from their defined scope. While capturing variabl
2 min read
Rust - A Case of Safe Concurrency Before looking at Rust itself, let's go back to 1992. Guido van Rossum, in an attempt to tackle race condition in CPython interpreter, added a lock famously known as the Global Interpreter Lock or GIL for short. Two and a half decades later, this was one of the main shortcomings for the Python inter
5 min read
Rust - Concept of Associated Items and Associated Type In Rust, we have a concept of associated items and associated types. Associated items use the type keyword while associated type uses type alias and functions for their implementation. Associated Items in Rust:Rust uses certain items and declares them in their traits or defines them while implement
4 min read
What are Block Scoped variables and functions in ES6 ? Block-scoped variables and functions are defined inside a particular block or inside curly { } braces and they can only be accessible inside that particular block or within that block. The block-scoped variables were first introduced in EcmaScript2015 or es6. ES6 introduces us with two keywords: let
3 min read
Shadowing Properties in JavaScript Shadowing properties in JavaScript refer to the scenario where a variable declared within a nested scope has the same name as a variable in its outer scope. This can lead to confusion and unexpected behaviour, as the inner variable may "shadow" the outer one, effectively hiding it from the outer sco
2 min read