Complete the code to create an Option with a value.
let some_number = Some([1]);The Some variant holds a value. Here, 5 is the value wrapped in Some.
Complete the code to extract the value from an Option using match.
match maybe_value {
Some(val) => println!("Value is: {}", [1]),
None => println!("No value"),
}Inside the Some(val) arm, val holds the extracted value.
Fix the error in unwrapping an Option safely.
if let [1] = optional_value { println!("Got a value"); } else { println!("No value"); }
The if let syntax matches Some(value) to unwrap the Option safely.
Fill both blanks to create a dictionary comprehension that filters Some values and unwraps them.
let numbers = vec![Some(1), None, Some(3)]; let unwrapped: Vec<_> = numbers.iter().filter_map(|&[1]| [2]).collect();
Use opt as the variable name and call unwrap() inside filter_map to get the values.
Fill all three blanks to create a function that returns the square of an Option<i32> if it has a value.
fn square_option(num: Option<i32>) -> Option<i32> {
match num {
[1] => Some([2] * [3]),
None => None,
}
}The function matches Some(x) and returns Some(x * x). For None, it returns None.
