How to Use Option for a Value That May Be Missing in Rust
Rust has no null. A value that may be missing is an Option: Some(value) when it's there, and None when it isn't. The compiler makes you deal with None before you use the value, for example with unwrap_or, which gives a default.
Syntax
let <name>: Option<T> = Some(<value>);
let <name>: Option<T> = None;
<name>.is_some()
<name>.is_none()
<name>.unwrap_or(<default>)
| Part | What it does |
|---|---|
Option<T> | A value of type T that may be missing, where T is the value's type, such as &str. |
Some(<value>) | An Option that holds a value. |
None | An Option that holds nothing. |
.is_some(), .is_none() | true when the Option holds a value, or when it doesn't. |
.unwrap_or(<default>) | The value when there is one, otherwise <default>, which must have the same type. |
The std::option documentation: "Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not." The Rust Book, The Option Enum, says why: "Rust does not have nulls, but it does have an enum that can encode the concept of a value being present or absent."
The Example
The example holds the nicknames of two users: one set a nickname, Addie, and the other didn't. It prints both Options, checks which one holds a value, and prints each nickname with a default for the missing one. Then a changed version calls a string method on the Option itself.
Prerequisites
- Rust, with Cargo. This guide uses Rust 1.100.1.
Step 1: Create the Project and Write the Code
cargo new demo
cd demo
cargo new demo creates a project with a src/main.rs. Replace its contents:

fn main() {
// One user set a nickname; another didn't.
let nickname: Option<&str> = Some("Addie");
let no_nickname: Option<&str> = None;
println!("{nickname:?}");
println!("{no_nickname:?}");
println!("{}", nickname.is_some());
println!("{}", no_nickname.is_none());
println!("{}", nickname.unwrap_or("no nickname"));
println!("{}", no_nickname.unwrap_or("no nickname"));
}
Option<&str>is a piece of text that may be missing.{nickname:?}prints theOptionwith the Debug format, which shows whether it'sSomeorNone.is_some()andis_none()check whether a value is there.unwrap_or("no nickname")gives the nickname, orno nicknamewhen there isn't one.
Step 2: Run It
cargo run -q
Some("Addie")
None
true
true
Addie
no nickname
-q keeps Cargo quiet, so only the program's output shows. The command is the same in zsh, bash and PowerShell.
Step 3: Call a String Method on the Option
Replace src/main.rs with a version that adds one line at the end, which calls to_uppercase() on the Option:
fn main() {
// One user set a nickname; another didn't.
let nickname: Option<&str> = Some("Addie");
let no_nickname: Option<&str> = None;
println!("{nickname:?}");
println!("{no_nickname:?}");
println!("{}", nickname.is_some());
println!("{}", no_nickname.is_none());
println!("{}", nickname.unwrap_or("no nickname"));
println!("{}", no_nickname.unwrap_or("no nickname"));
println!("{}", nickname.to_uppercase());
}
cargo run -q
error[E0599]: no method named `to_uppercase` found for enum `Option<T>` in the current scope
--> src/main.rs:15:29
|
15 | println!("{}", nickname.to_uppercase());
| ^^^^^^^^^^^^ method not found in `Option<&str>`
|
note: the method `to_uppercase` exists on the type `&str`
--> /rustc/20ddd01716a87c0fcf06ae4752d85a4abc15bcdf/library/alloc/src/str.rs:656:4
help: consider using `Option::expect` to unwrap the `&str` value, panicking if the value is an `Option::None`
|
15 | println!("{}", nickname.expect("REASON").to_uppercase());
| +++++++++++++++++
For more information about this error, try `rustc --explain E0599`.
error: could not compile `demo` (bin "demo") due to 1 previous error

What Happened
Some("Addie")andNone: the Debug format shows what eachOptionholds.trueandtrue:nicknameholds a value, andno_nicknamedoesn't.Addieandno nickname:unwrap_orgave the value fromSome, and the default forNone.- The changed version didn't compile. An
Option<&str>isn't a&str, so it doesn't have the methods of&str. The note says the method exists on&str, and the help suggestsexpect, which takes the value out but panics onNone.
Take the value out where it's there, and decide what happens when it isn't. In my checks, if let Some(name) = nickname with name.to_uppercase() inside printed ADDIE, and no_nickname.unwrap_or("no nickname").to_uppercase() printed NO NICKNAME.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling a method of the value on the Option, as in nickname.to_uppercase() | error[E0599]: no method named `to_uppercase` found for enum `Option<T>` in the current scope | Take the value out first, with if let Some(name) = nickname or unwrap_or |
Printing an Option with {} | error[E0277]: `Option<&str>` doesn't implement `std::fmt::Display` | Print it with {:?}, or print the value after taking it out |
unwrap() on an Option that may be None | With None, the program panics: called `Option::unwrap()` on a `None` value | Use unwrap_or with a default, or if let |
null for a missing value | error[E0425]: cannot find value `null` in this scope | Use None |
None in a variable of the value's own type, as in let nickname: &str = None; | error[E0308]: mismatched types: expected &str, found Option<_> | Declare the variable as Option<&str> |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error[E0624]: method `len` is private on an Option | len() was called on the Option, which has a private len of its own | Take the value out first, as shown in this fix |
A panic with the message you gave expect, such as a nickname is set | expect on an Option that was None | Keep expect for values that can only be missing because of a bug |
FAQ
How do I run code only when there is a value?
With if let: if let Some(name) = nickname { ... } runs its block only for Some, with the value in name. A match handles both cases: with Some(name) and None as its two arms, it printed no nickname for no_nickname.
How do I change the value and keep it in an Option?
With map: nickname.map(|name| name.to_uppercase()) gave Some("ADDIE"), and on no_nickname it gave None.
What about an Option of a String?
It works the same way. as_deref() turns an Option<String> into an Option<&str>, so nickname.as_deref().unwrap_or("no nickname") gave Addie, and no nickname for None.
Conclusion
An Option holds a value that may be missing: Some(value) or None. Check it with is_some or is_none, give it a default with unwrap_or, or take the value out with if let, and the compiler makes sure you never use a missing value by accident.
Additional Resources
- std::option and Option, in the Rust standard library documentation.
- The Option Enum, in The Rust Programming Language.
