Skip to main content

Fixed: "error[E0624]: method `len` is private" on an Option in Rust

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Terminal
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:

src/main.rs, which stores the nickname Addie in an Option and prints the result of calling len on that Option.

src/main.rs
fn main() {
let nickname: Option<&str> = Some("Addie");
println!("{}", nickname.len());
}

Run it:

Terminal
cargo run -q
Output
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

A zsh terminal where cargo run -q fails with error E0624, method len is private, with a note that the private method is defined in option.rs, a note that the method len exists on the type str, and a help that suggests calling expect before len.

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 len method that my code isn't allowed to call.
  • Where that len is. The first note points into the standard library, at library/core/src/option.rs, and says "private method defined here". The method the compiler found is a len of Option itself, which only the standard library may call. It isn't the len of &str at all.
  • What the compiler suggests. The second note says "the method len exists on the type &str", and the help suggests nickname.expect("REASON").len(): take the &str out of the Option, then call its len.
  • Whether it's a known problem. It is. Rust issue #143795, "Error message for private method of Option<T> is suboptimal", reported this case with Some(vec![1,2,3]) and len(). 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:

src/main.rs
fn main() {
let nickname: Option<&str> = Some("Addie");
if let Some(name) = nickname {
println!("{}", name.len());
} else {
println!("no nickname");
}
}
  • if let Some(name) = nickname runs the block only when nickname holds a value, and names that value name, a &str.
  • name.len() calls the len of &str, which is public.
  • else runs when nickname is None. With None, this program printed no nickname.

Three other ways worked in my checks:

  • nickname.map(|name| name.len()) keeps the answer in an Option: Some(5), or None when there's no nickname.
  • nickname.unwrap_or("").len() counts an empty text when there's no nickname: 5, or 0.
  • nickname.expect("a nickname is set").len(), the compiler's suggestion, gave 5. With None, though, the program stopped with a panic, a nickname is set. Use it only when a missing value is a bug.

The Result​

Terminal
cargo run -q
Output
5

A zsh terminal where cargo run -q prints 5 with no error.

The program compiled and printed 5, the length of Addie.

How to Avoid It​

  • Take the value out of an Option with if let, map or unwrap_or before you call the value's methods.
  • When the compiler says a method of an Option is private, look for the note that says the method exists on the type inside.
  • Keep expect for values that can't be missing unless the program has a bug.

Troubleshooting​

SymptomLikely causeFix
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 ownTake 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 scopeAnother method of the value inside was called on the OptionThe same fix: take the value out first
called `Option::unwrap()` on a `None` value when the program runsunwrap() on an Option that is NoneUse 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​