What Is Shadowing In Rust?

Shadowing can be confusing to beginners, but it’s an important part of writing clean and flexible Rust programs. It allows you to reuse names without creating unnecessary variables, which keeps your code small and expressive.

What Exactly Is Shadowing?

When we declare a variable using the let keyword, we can declare another variable with the same name, which “shadows” or hides the old one; it does not overwrite the original variable; instead, it creates a new binding in memory.

For example:

fn main() {
let name = "Evan";
println!("Original name: {}", name);

let name = 42; // Shadowing changes type to integer
println!("Shadowed name as number: {}", name);
}

Output of this program:

Original name: Evan
Shadowed name as number: 42

Here, the name variable first stores a string “Evan” and later becomes an integer “42”. This is possible because shadowing creates a new variable each time.

Why Shadowing Used In Real-Life Projects?

When we want to change the variable type or name without a new name, we use shadowing.

Suppose, sometimes we get a string value from a user, but in a further process can calculate an integer. So, without creating a new variable, we reuse the same name by shadowing.

You can understand it like, use the same name variable multiple times with new values that replace the old variable and don’t overwrite. Which means it is not a Type Conversation, but creates each new copy with the same name, so you should not write extra names.

When The Shadowing Used In Projects?

We can use the Shadowing in E-commerce sites to get inputs without problems.

APIs get string format from user inputs, when users input a product price or quantity, so it will insert as a string, but after it will automatically become an integer for making calculations of product prices.

Here the simple example:

fn main() {
let price = "1200"; // from API (string)
let quantity = "3"; // from user input (string)

// Shadow as integers for math
let price: f64 = price.parse().unwrap();
let quantity: i32 = quantity.parse().unwrap();

let total = price * quantity as f64;
println!("Total price: {}", total);
}

Output:

Total price: 3600

Simple Use case of Shadowing

Shadowing use case

Example 2: Age Calculate By Date of Birth

It will get string input from the it user and convert it into a number, and then calculate the DOB.

fn main() {
let birth_year = "1995"; // user form input (string)
let birth_year: i32 = birth_year.parse().unwrap(); // shadow as integer

let current_year = 2025;
let age = current_year - birth_year;

println!("Your age is: {}", age);
}

Output:

Shadowing examples

And remember, Shadowing is not only for integer, but it’s always used for:

  • String to Integer
  • Integer to Float
  • Value transformations like meters to kilometers
  • Same type modify into lowercase
let username = " Alice ";        // raw string from input
let username = username.trim(); // shadowed as trimmed string
let username = username.to_lowercase(); // shadowed again as lowercase String
println!("Clean username: {}", username);

Browsers forms, CLI (terminal) inputs, or mobile apps send all the values in text formats. If the user by mistake types “25a”, it should store in a string to get validation and an error message, same as you saw in the Use case diagram.

Also, most APIs (JSON, XML) send data in string form, even if it is a number or text. For example:

{ "price": "1200" }

Rust are the safe language this reason it first force to get as a string input, then parse the values and convert it into integer, so program can be secure and don’t crash.

Shadowing or Mutability Are The Same?

Most of the beginners is thinking shadowing and the mut keyword are the same. But they are not same.

  • mut allows you to change the value of a variable, but the type must remain the same.
  • Shadowing allows you to redeclare a variable with the same name, and you can also change its type.

Clear Basic Concepts Like:

Leave a Comment