Rust - Using Option enum for Error Handling Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 for using inner values. Options <T> uses pattern matches for querying whether the value is present or not and in case there aren't any, it references a nullable (None). Whenever there is a pattern match, the rust compiler assigns the box a value, and if there aren't any then the box is empty. Example 1: Rust #![allow(unused)] fn main() { let matching_digit = Some(100); fn processing_digit(i: i32) { println!("Inside Processing digit function!"); } fn processing_other_digit(i: i32) { println!("Inside Processing Other digit function!"); } let msg = match matching_digit { Some(x) if x < 20 => processing_digit(x), Some(x) => processing_digit(x), None => panic!(), }; } Output: Explanation: In this example, we have used a variable named matching_digit that holds a value passed by the Some() function. The function processing digit as well the function other_processing digit both accept 32-bit integers. Now, we pass a value of 100 and assign a condition to one of the Some parameters that if the value is less than 20 it will match and the processing_digit function would be called and if nothing is passed Rust will use None which would panic. Example 2: Rust fn find(search: &str, variable: char) -> Option<usize> {search.find(variable) } fn main() { let var_name = "GeeksforGeeks"; match find(var_name, 'G') { None => println!("Letter not found"), Some(_i) => println!("Letter found"), } } Output: Explanation: In this example, we are finding the letter 'G' initially and we are checking using Some() whether the value is present or not. If it is present then var_name matches with the find function and we print Letter found in the output. Similarly, for another example, if we search for the letter 'Z' we won't find it. Example 3: Rust fn find(search: &str, variable: char) -> Option<usize> { search.find(variable) } fn main() { let var_name = "GeeksforGeeks"; match find(var_name, 'Z') { None => println!("Letter not found"), Some(_i) => println!("Letter found"), } } Output: Comment More infoAdvertise with us Next Article Error Handling with the Either type in TypeScript S sayanc170 Follow Improve Article Tags : Technical Scripter Rust Technical Scripter 2022 Rust Keywords Similar Reads Rust - Error Handling An error is basically an unexpected behavior or event that may lead a program to produce undesired output or terminate abruptly. Errors are things that no one wants in their program. We can try to find and analyze parts of the program that can cause errors. Once we found those parts then we can defi 5 min read Error Handling in LISP The condition system in Lisp is one of its best features. It accomplishes the same task as the exception handling mechanisms in Java, Python, and C++ but is more adaptable. In reality, its adaptability goes beyond error handling since conditions, which are more flexible than exceptions, can represen 9 min read Rust - Unpacking Options Using "?" Operator 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. Imp 2 min read Error Handling with the Either type in TypeScript Error handling with the Either type in TypeScript allow a user to manage computations that can result in either a success (Right) or a failure (Left), allowing functions to explicitly handle both outcomes. This pattern helps in avoiding exceptions and promotes explicit handling of both successful an 3 min read Rust - Panic Error Handling Mechanism In Rust, the error-handling mechanism is via the use of the panic keyword. Rust handles error handling by grouping the errors into two categories - Recoverable errors and nonrecoverable errors. The former deals with errors related to file not found while the latter deals with errors related to code 2 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 Like