Rust की मूल बातें

Rust Functions and Return Values

Define functions with typed parameters and return a value without writing return.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • Parameters with types
  • Return type after ->
  • The last expression is returned
area(4, 5)
  1. 14, 5arguments
  2. 2width * heightthe body runs
  3. 320returned to the caller

Values go in, one value comes out

A function groups code under a name. In Rust you write the type of every parameter and, after ->, the type of the value the function returns.

The last expression of a function is its return value when it has no semicolon, so width * height is returned without the return keyword. format! builds a String the same way println! prints one.

मुख्य बातें

  1. Every parameter needs a type.
  2. -> u32 says what the function returns.
  3. A final expression without ; is the return value.

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

Rust
कोड चलाएँ →
main.rs
fn area(width: u32, height: u32) -> u32 {
    width * height
}

fn describe(name: &str, size: u32) -> String {
    format!("{name} covers {size} square meters")
}

fn main() {
    let size = area(4, 5);
    println!("{}", describe("The room", size));
}
इस उदाहरण के लिए Run बटन बनाएँ कोड चलाने का परिणाम
The room covers 20 square meters

अभ्यास

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

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

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

अगला: Binary Search →

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

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

सभी चैलेंज