Option Type का उपयोग
Option बिना कभी null उपयोग किए "यहां एक value हो सकती है, या यहां कुछ नहीं हो सकता" कहने का Rust का सुरक्षित तरीका है।In this page:
enum Option<T> {
Some(T),
None,
}
let value: Option<Type> = Some(v);
What Is Option
Option<T> दो variants वाला एक enum है: Some(value) जब एक value मौजूद है, और None जब यह absent है। यह कई दूसरी languages में पाए null concept को replace करता है।
उदाहरण: What Is Option
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `some_number` (a `Option<i32>`), set to `Some(5)`
let some_number: Option<i32> = Some(5);
// Declare an immutable variable `no_number` (a `Option<i32>`), set to `None`
let no_number: Option<i32> = None;
// Print "{:?} and {:?}" to the console, substituting in `some_number, no_number` (with a trailing newline)
println!("{:?} and {:?}", some_number, no_number);
}
Login to try C/C++/Java/PHP code in the editor
Matching on Option
match आपको एक Option की दोनों possibilities सुरक्षित रूप से handle करने देता है, Some case में inner value निकालते हुए और None case में absence explicitly handle करते हुए।
उदाहरण: Matching on Option
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `maybe_age` (a `Option<u32>`), set to `Some(25)`
let maybe_age: Option<u32> = Some(25);
// Match on `maybe_age` and branch by pattern
match maybe_age {
Some(age) => println!("Age is {}", age),
None => println!("No age provided"),
}
}
Login to try C/C++/Java/PHP code in the editor
Using unwrap_or for a Default
.unwrap_or(default) अगर मौजूद हो तो inner value return करता है, या अगर Option None है तो एक fallback default value, साधारण cases के लिए एक पूरे match की ज़रूरत से बचते हुए।
उदाहरण: Using unwrap_or for a Default
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `missing` (a `Option<i32>`), set to `None`
let missing: Option<i32> = None;
// Declare an immutable variable `value`, set to `missing.unwrap_or(0)`
let value = missing.unwrap_or(0);
// Print "Value: {}" to the console, substituting in `value` (with a trailing newline)
println!("Value: {}", value);
}
Login to try C/C++/Java/PHP code in the editor
Functions Returning Option
वे functions जिनके पास शायद return करने के लिए एक meaningful result न हो अपने return type के रूप में Option<T> उपयोग करते हैं, हर caller को None की possibility explicitly handle करने पर मजबूर करते हुए।
उदाहरण: Functions Returning Option
// Define the function `find_even` taking `numbers` and returning a `Option<i32>`
fn find_even(numbers: &[i32]) -> Option<i32> {
// Loop over `numbers`, binding each item to `&n`
for &n in numbers {
// Check whether `n % 2 == 0`
if n % 2 == 0 {
// Return `Some(n)` from this function
return Some(n);
}
}
None
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `nums`, set to `[1, 3, 4, 7]`
let nums = [1, 3, 4, 7];
// Match on `find_even(&nums)` and branch by pattern
match find_even(&nums) {
Some(n) => println!("Found even number: {}", n),
None => println!("No even number found"),
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी
Option<T>value को सीधे एकTकी तरह उपयोग करने की कोशिश करना, पहले इसे unwrap या match किए बिना। - किसी
Optionपर.unwrap()call करना जोNoneहो सकता है, जो program को runtime पर panic कराता है। - यह मान लेना कि Rust में दूसरी languages की तरह एक
nullvalue है -- Rust में जानबूझकर कोई null नहीं है;Option::Noneउस concept को सुरक्षित रूप से replace करता है।
Option<T>एक value represent करता है जो या तोSome(value)है याNone।- Rust में कोई null pointers नहीं हैं;
Optionआपको explicitly 'no value' case handle करने पर मजबूर करता है। match,if let, और.unwrap_or()जैसे combinator methods एकOptionको सुरक्षित रूप से handle करने के आम तरीके हैं।Optionउपयोग करना 'no value' की possibility को type system में move करता है, missing-value bugs को compile time पर पकड़ते हुए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: