let से Variables Declare करना
let शब्द यह बताने का तरीका है कि "मैं इस value को एक नाम के तहत याद रखना चाहता हूं।"let name = value;
let name: Type = value;
Basic let Bindings
let keyword एक नई variable binding बनाता है, एक नाम को एक value से जोड़ते हुए। एक बार assign होने के बाद, वह value बाद में उसी scope में कहीं भी पढ़ी जा सकती है।
उदाहरण: Basic let Bindings
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `language`, set to `"Rust"`
let language = "Rust";
// Print "Learning {}" to the console, substituting in `language` (with a trailing newline)
println!("Learning {}", language);
}
Login to try C/C++/Java/PHP code in the editor
Type Annotations
हालांकि Rust आमतौर पर किसी variable का type इसकी value से infer कर सकता है, आप एक colon के बाद एक explicit type annotation जोड़ सकते हैं। यह तब ज़रूरी है जब type infer न किया जा सके, और कभी-कभी clarity के लिए उपयोग होता है।
उदाहरण: Type Annotations
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `count` (an integer), set to `42`
let count: i32 = 42;
// Print "count = {}" to the console, substituting in `count` (with a trailing newline)
println!("count = {}", count);
}
Login to try C/C++/Java/PHP code in the editor
Immutability by Default
let से बनाई गई variables default रूप से immutable होती हैं, मतलब आप creation के बाद उन्हें एक नई value assign नहीं कर सकते। ऐसा करने की कोशिश एक compile error है, जो गलती से बदलाव रोकने का Rust का तरीका है।
उदाहरण: Immutability by Default
fn main() {
let pi = 3;
println!("pi is approximately {}", pi);
// pi = 4; // this line would fail to compile
}
Login to try C/C++/Java/PHP code in the editor
Scope of Variables
एक variable सिर्फ उस code block (curly braces) के अंदर valid है जहां इसे declare किया गया था। एक बार वह block खत्म होने पर, variable scope से बाहर हो जाती है और इसे और access नहीं किया जा सकता।
उदाहरण: Scope of Variables
// Entry point: execution of the program starts here
fn main() {
{
// Declare an immutable variable `inner`, set to `"only visible here"`
let inner = "only visible here";
// Print "{}" to the console, substituting in `inner` (with a trailing newline)
println!("{}", inner);
}
// Print "Outer scope continues" to the console (with a trailing newline)
println!("Outer scope continues");
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि plain
letसे declared variables default रूप से immutable हैं और उन्हें reassign नहीं किया जा सकता। - किसी variable को
letसे value दिए जाने से पहले उपयोग करने की कोशिश करना। - यह मान लेना कि
letको हमेशा एक explicit type चाहिए, जबकि Rust आमतौर पर इसे assigned value से infer कर सकता है।
letएक नाम को एक value से बांधता है; default रूप से binding immutable होती है।- Rust ज़्यादातर cases में types अपने आप infer करता है, लेकिन आप
let x: i32 = 5;से explicitly annotate कर सकते हैं। - किसी variable को उपयोग होने से पहले initialize किया जाना चाहिए।
- Bindings उस block
{ }तक scoped होती हैं जिसमें वे declare हुई हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: