Expressions और Statements में अंतर
In this page:
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
fn main() {
let x = 5; // this whole line is a statement
println!("x = {}", x);
}
Login to try C/C++/Java/PHP code in the editor
What Is an Expression
एक expression एक value तक evaluate होता है। Simple examples में 5 + 6 या square(4) जैसा एक function call शामिल है, दोनों एक usable value produce करते हैं।
उदाहरण: What Is an Expression
fn main() {
let sum = 5 + 6; // 5 + 6 is an expression
println!("sum = {}", sum);
}
Login to try C/C++/Java/PHP code in the editor
Blocks as Expressions
{ } में enclosed एक block खुद एक expression है: अगर इसकी final line में कोई trailing semicolon न हो, वह line की value पूरे block की value बन जाती है।
उदाहरण: Blocks as Expressions
fn main() {
let y = {
let a = 3;
a * a // no semicolon: this is the block's value
};
println!("y = {}", y);
}
Login to try C/C++/Java/PHP code in the editor
Why the Semicolon Matters
किसी expression के बाद एक semicolon जोड़ना इसे एक statement में बदल देता है जो unit type () तक evaluate होता है। यही कारण है कि किसी function की आखिरी line पर एक stray semicolon भूलना या जोड़ना यह बदल देता है कि यह एक value return करता है या नहीं।
उदाहरण: Why the Semicolon Matters
fn plus_one(x: i32) -> i32 {
x + 1 // no semicolon, so this value is returned
}
fn main() {
println!("{}", plus_one(4));
}
Login to try C/C++/Java/PHP code in the editor
- जो returned tail expression बनना था उसमें एक semicolon जोड़ना, गलती से इसे एक statement में बदलते हुए जो
()return करता है। - यह मान लेना कि एक
if/matchblock को एक value produce करने के लिए हमेशाreturnचाहिए, जब tail expression अक्सर काफी होता है। - किसी statement (जैसे एक
letbinding) का 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: