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

mut से Mutability

mut शब्द जोड़ना Rust को बताता है कि कोई value बाद में बदली जा सकती है।
Syntax
rust
let mut name = value;
name = new_value;

Declaring a Mutable Variable

let के बाद mut keyword जोड़ना variable की value को बाद में program में बदले जाने देता है। mut के बिना, variable को reassign करने की कोई भी कोशिश एक compile-time error है।

उदाहरण: Declaring a Mutable Variable

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `score`, initialized to `0`
    let mut score = 0;
    // Print "Starting score: {}" to the console, substituting in `score` (with a trailing newline)
    println!("Starting score: {}", score);
    score = 10;
    // Print "Updated score: {}" to the console, substituting in `score` (with a trailing newline)
    println!("Updated score: {}", score);
}

Mutating in a Loop

Mutable variables loops के अंदर विशेष रूप से उपयोगी हैं, जहां एक value को हर iteration पर accumulate या बदलना पड़ता है, जैसे एक running total।

उदाहरण: Mutating in a Loop

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `total`, initialized to `0`
    let mut total = 0;
    // Loop over `1..=5`, binding each item to `n`
    for n in 1..=5 {
        total += n;
    }
    // Print "Total: {}" to the console, substituting in `total` (with a trailing newline)
    println!("Total: {}", total);
}

Type Stays Fixed

भले ही एक mut variable की value बदल सकती है, इसका type binding की पूरी life के लिए fixed रहता है। आप किसी variable में एक string store नहीं कर सकते जिसे पहले एक integer assign किया गया था।

उदाहरण: Type Stays Fixed

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `count` (an integer), initialized to `1`
    let mut count: i32 = 1;
    count = count + 1;
    // Print "count is now {}" to the console, substituting in `count` (with a trailing newline)
    println!("count is now {}", count);
}

Immutable by Default, Opt-in Mutability

Rust ने immutable-by-default चुना ताकि, जब आप code पढ़ें, एक plain let binding इस बात की गारंटी हो कि इसकी value कभी नहीं बदलती -- programs को reason करना आसान बनाते हुए, खासकर जब code के कई हिस्से data share करते हों।

उदाहरण: Immutable by Default, Opt-in Mutability

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `fixed`, set to `100`
    let fixed = 100;
    // Declare a mutable variable `adjustable`, initialized to `100`
    let mut adjustable = 100;
    adjustable += 1;
    // Print "fixed: {}, adjustable: {}" to the console, substituting in `fixed, adjustable` (with a trailing newline)
    println!("fixed: {}, adjustable: {}", fixed, adjustable);
}
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. mut keyword भूल जाना और फिर किसी variable को reassign करने की कोशिश करते समय एक confusing compiler error पाना।
  2. यह मान लेना कि mut आपको किसी variable का type बदलने देता है, जबकि यह सिर्फ इसकी value बदलने देता है, type नहीं।
  3. सिर्फ उन variables को चिह्नित करने के बजाय जिन्हें असल में बदलने की ज़रूरत है, आदतन सब कुछ mut चिह्नित करना।
चैप्टर सारांश
  • किसी variable नाम से पहले mut जोड़ना इसकी value को बाद में reassign होने देता है।
  • किसी mutable variable का type बदल नहीं सकता, सिर्फ इसकी value।
  • Rust default रूप से immutability को प्रोत्साहित करता है ताकि code reason करने और concurrently चलने में सुरक्षित रहे।
  • अगर एक mut variable कभी mutate नहीं हुआ तो compiler warn करेगा।
🔒

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.