match से Pattern Matching
match आपके program को एक value की कई possible options के साथ तुलना करने और हर एक के लिए अलग code चलाने देता है।match value {
pattern1 => expression,
pattern2 | pattern3 => expression,
low..=high => expression,
_ => default_expression,
}
Basic match
match एक value की तुलना patterns की एक list से order में करता है और पहले matching pattern के लिए code execute करता है। यह दो से ज़्यादा possibilities पर branch करने के लिए Rust का primary tool है।
उदाहरण: Basic match
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `day`, set to `3`
let day = 3;
// Match on `day` and branch by pattern
match day {
1 => println!("Monday"),
2 => println!("Tuesday"),
3 => println!("Wednesday"),
_ => println!("Some other day"),
}
}
Login to try C/C++/Java/PHP code in the editor
Exhaustiveness
Rust compiler चाहता है कि हर match matched type की सभी possible values cover करे। _ wildcard pattern एक catch-all है जो explicitly listed न की गई किसी भी values के लिए यह requirement पूरा करता है।
उदाहरण: Exhaustiveness
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `letter_grade`, set to `'B'`
let letter_grade = 'B';
// Match on `letter_grade` and branch by pattern
match letter_grade {
'A' => println!("Excellent"),
'B' => println!("Good"),
'C' => println!("Average"),
_ => println!("Needs improvement"),
}
}
Login to try C/C++/Java/PHP code in the editor
Matching Multiple Values
किसी pattern के अंदर | operator एक single match arm को एक साथ कई अलग values handle करने देता है, identical code वाले repeated arms से बचते हुए।
उदाहरण: Matching Multiple Values
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `n`, set to `4`
let n = 4;
// Match on `n` and branch by pattern
match n {
1 | 3 | 5 | 7 => println!("{} is odd (from list)", n),
2 | 4 | 6 | 8 => println!("{} is even (from list)", n),
_ => println!("Out of range"),
}
}
Login to try C/C++/Java/PHP code in the editor
match as an Expression
if की तरह, match एक expression है और सीधे एक value produce कर सकता है, जिसे आमतौर पर arms में logic duplicate करने के बजाय एक variable को assign किया जाता है।
उदाहरण: match as an Expression
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `number`, set to `6`
let number = 6;
// Declare an immutable variable `description`; its value comes from the block below
let description = match number % 2 {
0 => "even",
_ => "odd",
};
// Print "{} is {}" to the console, substituting in `number, description` (with a trailing newline)
println!("{} is {}", number, description);
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि Rust में
matchexhaustive होना चाहिए -- type की हर possible value cover होनी चाहिए, नहीं तो यह compile नहीं होगा। matchमें_को बहुत जल्दी उपयोग करना, गलती से उन cases को पकड़ते हुए जिन्हें बाद में listed किसी ज़्यादा specific arm को handle करना चाहिए था।- C के
switchstatement की तरह match arms के बीच fall through करने की कोशिश करना -- Rust केmatcharms कभी fall through नहीं करते।
matchएक value की तुलना patterns की एक series से करता है और पहले matching वाले के लिए code चलाता है।- Matches exhaustive होने चाहिए;
_wildcard pattern बचे हुए किसी भी cases को पकड़ता है। - कई values एक arm share कर सकती हैं
|pattern उपयोग करके, जैसे1 | 2 => ...। matchएक expression है, इसलिए यहifकी तरह एक value produce कर सकता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: