Rust की मूल बातें

Rust Data Types: Integers, Floats, bool and char

Pick the right type for each value and convert between numbers with as.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • u8, i32, i64 and f64
  • bool and char
  • Converting with as
Value → type
30u8, a small whole number
-4.5f64, a decimal number
truebool
'A'char

Every value has a type known at compile time

Rust is statically typed: every value has a type that the compiler knows. Integers come in sizes such as u8 and i64, f64 holds decimal numbers, bool is true or false, and char is one Unicode character.

Rust never converts numbers silently, so age as f64 is needed before adding a float. Underscores in 1_000_000 only make the number easier to read. u8::MAX is the largest value a u8 can hold.

मुख्य बातें

  1. Integer types say their size: u8 holds 0 to 255.
  2. Rust never converts numbers silently: write age as f64.
  3. char is one Unicode character, written in single quotes.

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

Rust
कोड चलाएँ →
main.rs
fn main() {
    let age: u8 = 30;
    let temperature: f64 = -4.5;
    let is_ready: bool = true;
    let grade: char = 'A';
    let big = 1_000_000i64;

    let total = age as f64 + temperature;
    println!("{age} {temperature} {is_ready} {grade} {big}");
    println!("total = {total}");
    println!("u8 max = {}", u8::MAX);
}
इस उदाहरण के लिए Run बटन बनाएँ कोड चलाने का परिणाम
30 -4.5 true A 1000000
total = 25.5
u8 max = 255

अभ्यास

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

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

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

अगला: Operators and Arithmetic →

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

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

सभी चैलेंज