Rust की मूल बातें

Rust Operators and Arithmetic

Why is 17 / 5 equal to 3 in Rust? Learn arithmetic, comparison and logical operators.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • + - * / and %
  • Integer division
  • Comparisons with && and ||
Expression → result
17 / 53
17 % 52
17 as f64 / 5 as f643.4
2_i32.pow(10)1024

How Rust evaluates an expression

Operators combine values into expressions. Rust follows the usual math rules, with one thing to remember: dividing two integers drops the fraction.

17 / 5 is 3 because both numbers are integers, and 17 % 5 is the remainder 2. Converting both sides with as f64 keeps the fraction. && is true only when both comparisons are true, and pow raises a number to a power.

मुख्य बातें

  1. int / int drops the fraction: 17 / 5 is 3.
  2. % gives the remainder: 17 % 5 is 2.
  3. Convert with as f64 to keep the fraction.

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

Rust
कोड चलाएँ →
main.rs
fn main() {
    let a = 17;
    let b = 5;
    println!("{} {} {}", a + b, a - b, a * b);
    println!("{} {}", a / b, a % b);
    println!("{}", a as f64 / b as f64);
    println!("{}", a > b && b != 0);
    println!("{}", 2_i32.pow(10));
}
इस उदाहरण के लिए Run बटन बनाएँ कोड चलाने का परिणाम
22 12 85
3 2
3.4
true
1024

अभ्यास

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

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

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

अगला: Reading User Input →

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

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

सभी चैलेंज