Rust की मूल बातें

Rust Variables, mut, const and Shadowing

Variables in Rust cannot change unless you ask. Learn let, let mut, const and shadowing.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • let and let mut
  • Constants with const
  • Shadowing a variable
Declaration → what it allows
let language = "Rust"cannot change
let mut score = 10can change
const MAX_SCORE: u32 = 100fixed for the whole program
let score = score * 2 → a new variable, 30

Immutable by default

Variables in Rust cannot change unless you say so. let creates a variable, let mut creates one you can change, and const creates a value that is fixed for the whole program.

score is declared with mut, so score += 5 is allowed. The second let score creates a new variable with the same name, which is called shadowing. The braces in println! print the variables named inside them.

मुख्य बातें

  1. let creates a variable that cannot change.
  2. let mut allows the value to change.
  3. A second let with the same name creates a new variable (shadowing).

चलने योग्य Rust उदाहरण

Rust
कोड चलाएँ →
main.rs
fn main() {
    let language = "Rust";
    let mut score = 10;
    score += 5;

    const MAX_SCORE: u32 = 100;
    let score = score * 2; // shadowing creates a new variable

    println!("{language}: {score} of {MAX_SCORE}");
}
इस उदाहरण के लिए Run बटन बनाएँ कोड चलाने का परिणाम
Rust: 30 of 100

अभ्यास

उदाहरण को ऑनलाइन कंपाइलर में खोलें, कोई मान बदलें या एक पंक्ति जोड़ें, और चलाने से पहले आउटपुट का अनुमान लगाएँ। फिर कोड में जान-बूझकर गलती करें और कंपाइलर का संदेश पढ़ें।

यह पाठ पूरा कर लिया?

अपनी प्रगति ट्रैक करने के लिए इसे सीखा हुआ मार्क करें।

अगला: Data Types →

चैलेंज से अभ्यास करें

इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।

सभी चैलेंज