Variable Shadowing क्या है
In this page:
let name = value;
let name = new_expression_using(name);
Basic Shadowing
किसी पहले से मौजूद नाम के साथ फिर से let उपयोग करना एक बिल्कुल नई variable बनाता है जो पुरानी वाली को shadow (छुपाता) करती है। पुरानी value अब भी existed करती थी, लेकिन अब वह उस नाम के तहत accessible नहीं है।
उदाहरण: Basic Shadowing
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Shadowing Can Change Type
चूंकि shadowing बिल्कुल एक नई variable बनाता है, नई binding original से अलग type रखने के लिए free है, जो mut से plain mutation कभी नहीं allow करता।
उदाहरण: Shadowing Can Change Type
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Shadowing Within a Block
एक inner block के अंदर एक shadowed variable सिर्फ उस block की अवधि के लिए outer वाली को shadow करता है। एक बार block खत्म होने पर, outer variable की value फिर visible हो जाती है।
उदाहरण: Shadowing Within a Block
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Shadowing vs Mutability
Shadowing और mut अलग problems solve करते हैं: mut एक value को जगह पर बदलता है वही type रखते हुए, जबकि shadowing एक independent नई binding produce करता है, optionally एक नए type के साथ, कभी mut की ज़रूरत के बिना।
उदाहरण: Shadowing vs Mutability
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- Shadowing को mutation के साथ confuse करना -- shadowing बिल्कुल एक नई variable बनाता है, यह पुरानी वाली को जगह पर नहीं बदलता।
- यह सोचना कि shadowing को
mutचाहिए; असल में shadowing plain immutableletbindings के साथ काम करती है। - किसी 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: