0
0
Rustprogramming~15 mins

Variable shadowing in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Variable shadowing
๐Ÿ“– Scenario: Imagine you are organizing a small event and need to keep track of the number of guests. Sometimes, you update the count temporarily for calculations without changing the original number.
๐ŸŽฏ Goal: You will create a variable to hold the initial guest count, then shadow it with a new value to show how variable shadowing works in Rust. Finally, you will print the final guest count.
๐Ÿ“‹ What You'll Learn
Create a variable called guest_count with the value 50.
Shadow the variable guest_count by creating a new variable with the same name and assign it the value guest_count + 10.
Print the value of guest_count.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Variable shadowing helps when you want to reuse a variable name for updated values without changing the original variable's mutability.
๐Ÿ’ผ Career
Understanding shadowing is important for writing clear and safe Rust code, which is valuable in systems programming and software development jobs.
Progress0 / 4 steps
1
Create the initial guest count variable
Create a variable called guest_count and set it to 50.
Rust
Need a hint?

Use let guest_count = 50; to create the variable.

2
Shadow the guest_count variable
Shadow the variable guest_count by creating a new variable with the same name and assign it the value guest_count + 10.
Rust
Need a hint?

Use let guest_count = guest_count + 10; to shadow the variable.

3
Print the guest_count variable
Use println! to print the value of guest_count.
Rust
Need a hint?

Use println!("{}", guest_count); to print the value.

4
Run the program to see the output
Run the program and observe the printed output showing the final guest count.
Rust
Need a hint?

The output should be 60 because the variable was shadowed with guest_count + 10.