[Rust] Option vs Result
Option
and Result
are two very central enums in Rust, and they are used for error handling and for representing the absence of a value. Here is a breakdown of both:
-
Option
:- An
Option
represents an optional value: everyOption
is eitherSome
and contains a value, orNone
, and does not. Option
is typically used when a function may succeed or fail, and you are only interested in the success case and don't care why the function failed.Option
is defined as follows:
enum Option<T> { Some(T), None, }
Here
T
is the type of the value to be stored in theOption
. - An
-
Result
:- A
Result
is a type that represents either success (Ok
) or failure (Err
). - Unlike
Option
,Result
does not assume failure is an "expected" or "uninteresting" case: it allows you to handle failures as well as successes. Result
is typically used when a function may succeed or fail, and you want to handle both cases.Result
is defined as follows:
enum Result<T, E> { Ok(T), Err(E), }
Here
T
is the type of the value to be returned in the success case (inside anOk
), andE
is the type of the value to be returned in the failure case (inside anErr
). - A
In general, you should use Result
when you want to understand what the error was, and you should use Option
when you don't care what the error was, only whether or not it occurred.
Some examples.
Option<T>
:
Suppose we have a function that finds a person's age based on their name in a predefined list. If the person is not in the list, it will return None
.
use std::collections::HashMap;
fn find_age(people: &HashMap<String, u8>, name: &str) -> Option<u8> {
people.get(name).cloned()
}
fn main() {
let mut people = HashMap::new();
people.insert("Alice".to_string(), 20);
people.insert("Bob".to_string(), 30);
match find_age(&people, "Alice") {
Some(age) => println!("Alice is {} years old.", age),
None => println!("I don't know Alice's age."),
}
match find_age(&people, "Charlie") {
Some(age) => println!("Charlie is {} years old.", age),
None => println!("I don't know Charlie's age."),
}
}
Result<T, E>
:
Suppose we have a function that tries to divide two numbers. If the divisor is zero, it will return an error.
fn divide(numerator: f64, denominator: f64) -> Result<f64, &'static str> {
if denominator == 0.0 {
Err("Cannot divide by zero")
} else {
Ok(numerator / denominator)
}
}
fn main() {
match divide(10.0, 2.0) {
Ok(result) => println!("10 divided by 2 is {}.", result),
Err(err) => println!("Error: {}", err),
}
match divide(10.0, 0.0) {
Ok(result) => println!("10 divided by 0 is {}.", result),
Err(err) => println!("Error: {}", err),
}
}
In the Option
example, we only care about whether we found the age or not. In the Result
example, we care about why the division failed, so we use a Result
to get an error message in case of failure.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-05-24 [Next.js] Override the Default Next.js Document
2022-05-24 [Next.js] Override the Default App Component in Next.js
2022-05-24 [Next.js] Serve Optimized Images Using the Next.js Image Component
2021-05-24 [Cloud DA] Serverless Framework with AWS - Part 1: DynamoDB & ApiGateway
2021-05-24 [Cloud DA] Serverless Framework with AWS - Part 0: Serverless Project structure
2021-05-24 [AWS] Lambda Middleware
2018-05-24 [Cypress] Test React’s Controlled Input with Cypress Selector Playground