if/else के Expressions
if और else आपके program को यह चुनने देते हैं कि कोई चीज़ true है या false, इसके आधार पर एक अलग path अपनाना।if condition {
// body
} else if condition2 {
// body
} else {
// body
}
let name = if condition { value1 } else { value2 };
Basic if/else
एक if expression एक boolean condition evaluate करता है और associated block सिर्फ तब चलाता है जब यह true हो; एक optional else block अन्यथा चलता है। Condition के आस-पास कोई parentheses की ज़रूरत नहीं, C या Java के विपरीत।
उदाहरण: Basic if/else
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `number`, set to `7`
let number = 7;
// Check whether `number > 5`
if number > 5 {
// Print "Greater than five" to the console (with a trailing newline)
println!("Greater than five");
// Otherwise, run this branch
} else {
// Print "Five or less" to the console (with a trailing newline)
println!("Five or less");
}
}
Login to try C/C++/Java/PHP code in the editor
else if Chains
कई conditions को else if से chain किया जा सकता है, top से bottom तक order में जांचे गए। पहला matching branch चलता है, और बाकी skip हो जाते हैं।
उदाहरण: else if Chains
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `grade`, set to `85`
let grade = 85;
// Check whether `grade >= 90`
if grade >= 90 {
// Print "A" to the console (with a trailing newline)
println!("A");
// Otherwise, if `grade >= 80`, run this branch
} else if grade >= 80 {
// Print "B" to the console (with a trailing newline)
println!("B");
// Otherwise, run this branch
} else {
// Print "C or below" to the console (with a trailing newline)
println!("C or below");
}
}
Login to try C/C++/Java/PHP code in the editor
if as an Expression
चूंकि if एक expression है, सिर्फ एक statement नहीं, यह सीधे एक value produce कर सकता है, जिसे आप एक variable को assign कर सकते हैं। दोनों branches को एक ही type की values return करनी चाहिए।
5 और 6 के बाद कोई semicolons note करें -- वे हर block की tail expressions हैं।उदाहरण: if as an Expression
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `condition`, set to `true`
let condition = true;
// Declare an immutable variable `number`, set to `if condition { 5 } else { 6 }`
let number = if condition { 5 } else { 6 };
// Print "number is {}" to the console, substituting in `number` (with a trailing newline)
println!("number is {}", number);
}
Login to try C/C++/Java/PHP code in the editor
Condition Must Be a Bool
Rust चाहता है कि if condition एक असली bool value हो; यह implicitly किसी nonzero integer को true नहीं मानेगा, C के विपरीत। यह mistyped conditions से bugs की एक पूरी category रोकता है।
उदाहरण: Condition Must Be a Bool
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `count`, set to `3`
let count = 3;
// Check whether `count != 0`
if count != 0 {
// Print "count is non-zero: {}" to the console, substituting in `count` (with a trailing newline)
println!("count is non-zero: {}", count);
}
}
Login to try C/C++/Java/PHP code in the editor
- Condition को
if (x > 5)जैसे parentheses में wrap करना, जो Rust में अनावश्यक और unidiomatic है। - यह भूल जाना कि
ifएक expression है और एक value return कर सकता है, फिर extralet mutvariables के साथ अत्यधिक verbose code लिखना। ifको एक expression के रूप में उपयोग करते समयifऔरelsebranches के बीच types mix करना, जो compile होने में fail होता है।
if/else if/elseboolean conditions पर branch करते हैं; condition के आस-पास कोई parentheses ज़रूरी नहीं।- Rust में
ifएक expression है, इसलिए यह एक value produce कर सकता है जो सीधे किसी variable को assign होती है। - एक expression के रूप में उपयोग किए गए
ifके सभी branches को एक जैसे type तक evaluate होना चाहिए। - Condition एक
boolहोना चाहिए -- Rust numbers को implicitly booleans में convert नहीं करता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: