mut से Mutability
mut शब्द जोड़ना Rust को बताता है कि कोई value बाद में बदली जा सकती है।In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Mutating in a Loop
Mutable variables loops के अंदर विशेष रूप से उपयोगी हैं, जहां एक value को हर iteration पर accumulate या बदलना पड़ता है, जैसे एक running total।
उदाहरण: Mutating in a Loop
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Type Stays Fixed
भले ही एक mut variable की value बदल सकती है, इसका type binding की पूरी life के लिए fixed रहता है। आप किसी variable में एक string store नहीं कर सकते जिसे पहले एक integer assign किया गया था।
उदाहरण: Type Stays Fixed
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
mutkeyword भूल जाना और फिर किसी variable को reassign करने की कोशिश करते समय एक confusing compiler error पाना।- यह मान लेना कि
mutआपको किसी variable का type बदलने देता है, जबकि यह सिर्फ इसकी value बदलने देता है, type नहीं। - सिर्फ उन variables को चिह्नित करने के बजाय जिन्हें असल में बदलने की ज़रूरत है, आदतन सब कुछ
mutचिह्नित करना।
- किसी variable नाम से पहले
mutजोड़ना इसकी value को बाद में reassign होने देता है। - किसी mutable variable का type बदल नहीं सकता, सिर्फ इसकी value।
- Rust default रूप से immutability को प्रोत्साहित करता है ताकि code reason करने और concurrently चलने में सुरक्षित रहे।
- अगर एक
mutvariable कभी mutate नहीं हुआ तो compiler warn करेगा।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: