Rust - Unpacking Options Using "?" Operator Last Updated : 15 Sep, 2022 Comments Improve Suggest changes Like Article Like Report In Rust, there is a concept of the match and some. Match refers to a condition where an enum (type T) is foundNone refers to the condition when no element is found. The match statement is responsible for the explicit handling of this situation and unwraps statement is used for implicit handling. Implicit handling returns panic or an inner element. Example 1: Rust // Rust code for "?" operator fn func_name(choice: Option<&str>) { match choice { Some("dsa") => println!("lets practice DSA"), Some(gfg) => println!("Lets buy {} courses", gfg), None => println!("Lets learn Rust from GFG"), } } fn main() { let dsa = Some("dsa"); let gfg = Some("gfg"); let void = None; func_name(dsa); func_name(gfg); func_name(void); } Output: Unpacking Options with "?": Unpack options are done by match statements but we use the "?" operator to simplify things. Considering that 'var' is an option then by evaluating 'var' ? returns a value when 'var' is some otherwise, nothing is returned i.e. None gets returned. Here, if the choice is `None`, then it returns `None` and if the choice is `Some' then it gets assigned to next_choice. Example 2: Rust // Rust program Unpacking Options with "?" #[allow(dead_code)] fn main() { fn func_name (choice: Option<u8>) -> Option<String> { let next_choice: u8 = choice ? + 1; Some(format!("Lets buy {} courses", next_choice)) }} Output: Comment More infoAdvertise with us Next Article Rust - Using Option enum for Error Handling S sayanc170 Follow Improve Article Tags : Rust Rust-Operators Similar Reads Rust - Operator Overloading In Rust, we have a concept of operator overloading. This is generally performed by Traits. For making use of operator overloading, we import core::ops in Rust. Core:: ops is a module in Rust that allows the traits to overload the operators in our code. By default, most of the traits are automaticall 2 min read Rust - Using Option<T> enum for Error Handling In Rust, while coding the focus is mainly on performance, safety, and concurrency. It specifically uses enums like Option<T> and Result<T> for handling errors. Option type in Rust generally denotes option values that have two attributes: Some and None. The option type creates an i32 box 2 min read Rust Use Declaration In Rust, we have a concept of the 'use' keyword. This keyword is used for importing or at times renaming items from other different modules or crates. Use the keyword to find its usage when we are required to import items or rename items from crates or modules in Rust programs. For precisely calling 4 min read Rust - Iterator Trait Rust is a systems programming language focusing on speed, concurrency, and ensuring safe code. The iterator trait in Rust is used to iterate over Rust collections like arrays. Syntax: //pub refers to the trait being declared public pub trait Iterator { // type refers to the elements type over which 3 min read Rust - Formatting Print Statements In Rust, we use macros to print formatted texts in the editor. Macros provide functionality similar to functions but without the runtime cost. Print statements are used extensively in programming. They are used for prompting users for information or for debugging the program. Macros in Rust end with 3 min read How to avoid unused Variable warning in Rust? Rust is a modern system programming language mainly focusing on safety, performance, and concurrency. It eliminates many classes of bugs at compile time through Its ownership system which ensures memory safety without a garbage collector. In this article, we explain how to avoid unused variable warn 4 min read Like