0
0
Rustprogramming~15 mins

Scope of variables in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Scope of Variables in Rust
๐Ÿ“– Scenario: Imagine you are organizing a small event and need to keep track of the number of guests in different rooms. You want to understand how variables work inside and outside of blocks in Rust.
๐ŸŽฏ Goal: You will create variables inside and outside blocks to see how their scope affects their accessibility and values.
๐Ÿ“‹ What You'll Learn
Create a variable called guests with the value 10 outside any block.
Create a block and inside it create a variable called guests with the value 5.
Print the value of guests inside the block.
Print the value of guests outside the block.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Understanding variable scope helps you write clear and bug-free programs by controlling where data can be accessed or changed.
๐Ÿ’ผ Career
Many programming jobs require knowledge of variable scope to manage memory and avoid errors in complex code.
Progress0 / 4 steps
1
Create the outer variable guests
Create a variable called guests and set it to 10 outside any block.
Rust
Need a hint?

Use let guests = 10; to create the variable.

2
Create an inner block with a new guests variable
Inside the main function, create a block using curly braces { }. Inside this block, create a new variable called guests and set it to 5.
Rust
Need a hint?

Use curly braces to create a block and inside it write let guests = 5;.

3
Print the inner guests value inside the block
Inside the inner block, add a println! statement to print the value of guests.
Rust
Need a hint?

Use println!("Guests inside block: {}", guests); to print the value.

4
Print the outer guests value outside the block
After the inner block, add a println! statement to print the value of the outer guests variable.
Rust
Need a hint?

Use println!("Guests outside block: {}", guests); after the block to print the outer variable.