← Back to Rust Course | Chapter 4: Functions | Lesson 3 of 6

Expressions और Statements में अंतर

Rust में, code की कुछ lines एक value वापस देती हैं (expressions) और कुछ सिर्फ कुछ करती हैं और खत्म हो जाती हैं (statements)।
Syntax
rust
let name = {
    statement;
    expression  // no semicolon: block value
};

What Is a Statement

एक statement एक action perform करता है लेकिन खुद एक value तक evaluate नहीं होता। let के साथ Variable declarations statements हैं -- आप let x = 5; को एक value के रूप में उपयोग नहीं कर सकते।

उदाहरण: What Is a Statement

markup
fn main() {
    let x = 5; // this whole line is a statement
    println!("x = {}", x);
}

What Is an Expression

एक expression एक value तक evaluate होता है। Simple examples में 5 + 6 या square(4) जैसा एक function call शामिल है, दोनों एक usable value produce करते हैं।

उदाहरण: What Is an Expression

markup
fn main() {
    let sum = 5 + 6; // 5 + 6 is an expression
    println!("sum = {}", sum);
}

Blocks as Expressions

{ } में enclosed एक block खुद एक expression है: अगर इसकी final line में कोई trailing semicolon न हो, वह line की value पूरे block की value बन जाती है।

उदाहरण: Blocks as Expressions

markup
fn main() {
    let y = {
        let a = 3;
        a * a // no semicolon: this is the block's value
    };
    println!("y = {}", y);
}

Why the Semicolon Matters

किसी expression के बाद एक semicolon जोड़ना इसे एक statement में बदल देता है जो unit type () तक evaluate होता है। यही कारण है कि किसी function की आखिरी line पर एक stray semicolon भूलना या जोड़ना यह बदल देता है कि यह एक value return करता है या नहीं।

उदाहरण: Why the Semicolon Matters

markup
fn plus_one(x: i32) -> i32 {
    x + 1 // no semicolon, so this value is returned
}

fn main() {
    println!("{}", plus_one(4));
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. जो returned tail expression बनना था उसमें एक semicolon जोड़ना, गलती से इसे एक statement में बदलते हुए जो () return करता है।
  2. यह मान लेना कि एक if/match block को एक value produce करने के लिए हमेशा return चाहिए, जब tail expression अक्सर काफी होता है।
  3. किसी statement (जैसे एक let binding) का result किसी दूसरे variable को assign करने की कोशिश करना, क्योंकि statements एक usable value produce नहीं करते।
चैप्टर सारांश
  • Statements एक action perform करते हैं और value return नहीं करते; ये आमतौर पर एक semicolon से खत्म होते हैं।
  • Expressions एक value तक evaluate होते हैं, जैसे 5 + 6 या एक block जो बिना semicolon वाली value-produce करने वाली line पर खत्म होता है।
  • Blocks { ... } खुद expressions हैं अगर उनकी आखिरी line में कोई semicolon न हो।
  • इस distinction को समझना यह explain करता है कि semicolon हटाना या जोड़ना किसी function के behavior को क्यों बदल सकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.