Fixed: "error[E0624]: method `len` is private" on an Option in Rust
Rust stopped with error[E0624]: method `len` is private when I called .len() on an Option<&str>. Option has a private len method of its own, so the compiler found that one instead of the text's. Take the text out of the Option first, and its len works.
What I Was Doing
I was writing the example for a guide on Rust's Option. To show that an Option<&str> doesn't have the methods of the text inside it, I called a string method on the Option itself, and the first one I picked was len().
The example below is that step on its own: an Option<&str> that holds the nickname Addie, and a call to len() on it.
What I Expected
An error saying that Option has no method named len. Another string method called on the same Option, to_uppercase(), gave exactly that kind of error: error[E0599]: no method named `to_uppercase` found for enum `Option<T>` in the current scope.
What Happened Instead
Create a project:
cargo new demo
cd demo
cargo new demo creates a project in a folder named demo, with its code in src/main.rs, and cd demo moves into it.
Replace the contents of src/main.rs with:

fn main() {
let nickname: Option<&str> = Some("Addie");
println!("{}", nickname.len());
}
Run it:
cargo run -q
error[E0624]: method `len` is private
--> src/main.rs:3:29
|
3 | println!("{}", nickname.len());
| ^^^ private method
|
--> /rustc/20ddd01716a87c0fcf06ae4752d85a4abc15bcdf/library/core/src/option.rs:810:4
|
= note: private method defined here
note: the method `len` exists on the type `&str`
--> /rustc/20ddd01716a87c0fcf06ae4752d85a4abc15bcdf/library/core/src/str/mod.rs:155:4
help: consider using `Option::expect` to unwrap the `&str` value, panicking if the value is an `Option::None`
|
3 | println!("{}", nickname.expect("REASON").len());
| +++++++++++++++++
For more information about this error, try `rustc --explain E0624`.
error: could not compile `demo` (bin "demo") due to 1 previous error

The program didn't compile. The error code was E0624, not E0599, and the message called len private, although the len of &str is public.
What I Checked
- The error code. The E0624 page of the Rust error codes index says: "A private item was used outside of its scope." So the compiler had found a
lenmethod that my code isn't allowed to call. - Where that
lenis. The first note points into the standard library, atlibrary/core/src/option.rs, and says "private method defined here". The method the compiler found is alenofOptionitself, which only the standard library may call. It isn't thelenof&strat all. - What the compiler suggests. The second note says "the method
lenexists on the type&str", and the help suggestsnickname.expect("REASON").len(): take the&strout of theOption, then call itslen. - Whether it's a known problem. It is. Rust issue #143795, "Error message for private method of
Option<T>is suboptimal", reported this case withSome(vec![1,2,3])andlen(). The pull request that closed it, #144376, added the note and the help, and explains: "When a method isn't available, we emit E0599, but when it is private we emit E0624." The error code stays E0624 on purpose, and the note and the help point to the fix.
So the code called len() on the Option, not on the text inside it. Every method of the text needs the text first.
The Fix
Take the text out of the Option and call len() on that. if let does it only when there is a value, so the program also handles a missing nickname:
fn main() {
let nickname: Option<&str> = Some("Addie");
if let Some(name) = nickname {
println!("{}", name.len());
} else {
println!("no nickname");
}
}
if let Some(name) = nicknameruns the block only whennicknameholds a value, and names that valuename, a&str.name.len()calls thelenof&str, which is public.elseruns whennicknameisNone. WithNone, this program printedno nickname.
Three other ways worked in my checks:
nickname.map(|name| name.len())keeps the answer in anOption:Some(5), orNonewhen there's no nickname.nickname.unwrap_or("").len()counts an empty text when there's no nickname:5, or0.nickname.expect("a nickname is set").len(), the compiler's suggestion, gave5. WithNone, though, the program stopped with a panic,a nickname is set. Use it only when a missing value is a bug.
The Result
cargo run -q
5

The program compiled and printed 5, the length of Addie.
How to Avoid It
- Take the value out of an
Optionwithif let,maporunwrap_orbefore you call the value's methods. - When the compiler says a method of an
Optionis private, look for the note that says the method exists on the type inside. - Keep
expectfor values that can't be missing unless the program has a bug.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error[E0624]: method `len` is private on an Option<&str>, Option<String> or Option<Vec<i32>> | len() was called on the Option, which has a private len of its own | Take the value out first, as in if let Some(name) = nickname |
error[E0599]: no method named `to_uppercase` found for enum `Option<T>` in the current scope | Another method of the value inside was called on the Option | The same fix: take the value out first |
called `Option::unwrap()` on a `None` value when the program runs | unwrap() on an Option that is None | Use if let, or unwrap_or with a default |
FAQ
Why is the error E0624 and not E0599?
Because Option does have a method named len, and it's private. The pull request that added the note puts it this way: "When a method isn't available, we emit E0599, but when it is private we emit E0624." Option has no method named to_uppercase at all, so calling that one gives E0599.
Does it happen with an Option of a String or a Vec too?
Yes. In my checks, len() on an Option<String> and on an Option<Vec<i32>> gave the same error[E0624]: method `len` is private, with notes that the method exists on String and on Vec<i32>.
How do I check whether the Option holds a value at all?
With is_some(), which gave true here. nickname.iter().len() compiles too, but it gave 1: the number of values in the Option, not the length of the text.
Conclusion
error[E0624]: method `len` is private on an Option means the code called len() on the Option instead of on the value inside it. Take the value out first, with if let, map or unwrap_or, and call len() on that.
Additional Resources
- Error code E0624, in the Rust error codes index.
- Rust issue #143795 and pull request #144376, which added the note and the help.
- Option, in the Rust standard library documentation.
