← Back to Rust Course | Chapter 2: Variables & Types | Lesson 4 of 7

Variable Shadowing क्या है

Shadowing आपको उसी नाम को एक नई value के लिए reuse करने देता है, कुछ-कुछ एक पुराने note पर एक नया लिखने जैसा।
Syntax
rust
let name = value;
let name = new_expression_using(name);

Basic Shadowing

किसी पहले से मौजूद नाम के साथ फिर से let उपयोग करना एक बिल्कुल नई variable बनाता है जो पुरानी वाली को shadow (छुपाता) करती है। पुरानी value अब भी existed करती थी, लेकिन अब वह उस नाम के तहत accessible नहीं है।

उदाहरण: Basic Shadowing

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `x`, set to `5`
    let x = 5;
    // Declare an immutable variable `x`, set to `x + 1`
    let x = x + 1;
    // Declare an immutable variable `x`, set to `x * 2`
    let x = x * 2;
    // Print "x is {}" to the console, substituting in `x` (with a trailing newline)
    println!("x is {}", x);
}

Shadowing Can Change Type

चूंकि shadowing बिल्कुल एक नई variable बनाता है, नई binding original से अलग type रखने के लिए free है, जो mut से plain mutation कभी नहीं allow करता।

उदाहरण: Shadowing Can Change Type

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `spaces`, set to `"   "`
    let spaces = "   ";
    // Declare an immutable variable `spaces`, set to `spaces.len()`
    let spaces = spaces.len();
    // Print "Number of spaces: {}" to the console, substituting in `spaces` (with a trailing newline)
    println!("Number of spaces: {}", spaces);
}

Shadowing Within a Block

एक inner block के अंदर एक shadowed variable सिर्फ उस block की अवधि के लिए outer वाली को shadow करता है। एक बार block खत्म होने पर, outer variable की value फिर visible हो जाती है।

उदाहरण: Shadowing Within a Block

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `x`, set to `1`
    let x = 1;
    {
        // Declare an immutable variable `x`, set to `x + 10`
        let x = x + 10;
        // Print "Inner x: {}" to the console, substituting in `x` (with a trailing newline)
        println!("Inner x: {}", x);
    }
    // Print "Outer x: {}" to the console, substituting in `x` (with a trailing newline)
    println!("Outer x: {}", x);
}

Shadowing vs Mutability

Shadowing और mut अलग problems solve करते हैं: mut एक value को जगह पर बदलता है वही type रखते हुए, जबकि shadowing एक independent नई binding produce करता है, optionally एक नए type के साथ, कभी mut की ज़रूरत के बिना।

उदाहरण: Shadowing vs Mutability

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `value`, set to `"42"`
    let value = "42";
    // Declare an immutable variable `value` (an integer), set to `value.parse().unwrap()`
    let value: i32 = value.parse().unwrap();
    // Print "Parsed value: {}" to the console, substituting in `value` (with a trailing newline)
    println!("Parsed value: {}", value);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Shadowing को mutation के साथ confuse करना -- shadowing बिल्कुल एक नई variable बनाता है, यह पुरानी वाली को जगह पर नहीं बदलता।
  2. यह सोचना कि shadowing को mut चाहिए; असल में shadowing plain immutable let bindings के साथ काम करती है।
  3. किसी variable का type बार-बार बदलने के लिए shadowing को इस तरह overuse करना जो code को follow करने में confusing बना दे।
चैप्टर सारांश
  • Shadowing let से एक नई variable declare करता है जो एक मौजूदा नाम reuse करती है, पिछली वाली को छुपाते हुए।
  • Shadowing उस नाम के तहत stored value का type बदल सकता है, mutation के विपरीत।
  • हर shadowed variable इसके scope के आखिर तक या फिर से shadow होने तक मौजूद रहता है।
  • एक meaningful नाम रखते हुए एक value को step by step transform करने के लिए Shadowing उपयोगी है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.