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

let से Variables Declare करना

let शब्द यह बताने का तरीका है कि "मैं इस value को एक नाम के तहत याद रखना चाहता हूं।"
Syntax
rust
let name = value;
let name: Type = value;

Basic let Bindings

let keyword एक नई variable binding बनाता है, एक नाम को एक value से जोड़ते हुए। एक बार assign होने के बाद, वह value बाद में उसी scope में कहीं भी पढ़ी जा सकती है।

उदाहरण: Basic let Bindings

markup
// 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);
}

Type Annotations

हालांकि Rust आमतौर पर किसी variable का type इसकी value से infer कर सकता है, आप एक colon के बाद एक explicit type annotation जोड़ सकते हैं। यह तब ज़रूरी है जब type infer न किया जा सके, और कभी-कभी clarity के लिए उपयोग होता है।

उदाहरण: Type Annotations

markup
// 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);
}

Immutability by Default

let से बनाई गई variables default रूप से immutable होती हैं, मतलब आप creation के बाद उन्हें एक नई value assign नहीं कर सकते। ऐसा करने की कोशिश एक compile error है, जो गलती से बदलाव रोकने का Rust का तरीका है।

Note: किसी immutable variable के लिए reassignment line को locally uncomment करके देखें -- compiler error message बहुत descriptive है।

उदाहरण: Immutability by Default

markup
fn main() {
    let pi = 3;
    println!("pi is approximately {}", pi);
    // pi = 4; // this line would fail to compile
}

Scope of Variables

एक variable सिर्फ उस code block (curly braces) के अंदर valid है जहां इसे declare किया गया था। एक बार वह block खत्म होने पर, variable scope से बाहर हो जाती है और इसे और access नहीं किया जा सकता।

उदाहरण: Scope of Variables

markup
// 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");
}
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. यह भूल जाना कि plain let से declared variables default रूप से immutable हैं और उन्हें reassign नहीं किया जा सकता।
  2. किसी variable को let से value दिए जाने से पहले उपयोग करने की कोशिश करना।
  3. यह मान लेना कि 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:

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.