Answer#
A method call value.name() looks for name on the type of value, on traits implemented for it that are in scope, and on what the type dereferences to. E0599 says none matched. There are four usual reasons, in order of how often they happen.
How to do it#
- A typo or a name from another language. The compiler lists similar names:
fn main() {
let name = "ada";
println!("{}", name.to_upper());
}error[E0599]: no method named `to_upper` found for reference `&str` in the current scope
--> src/main.rs:3:25
|
3 | println!("{}", name.to_upper());
| ^^^^^^^^
|
help: there is a method `to_uppercase` with a similar name
|
3 | println!("{}", name.to_uppercase());
| ++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0599`.- Use the suggested name. Rust calls it
to_uppercase:
fn main() {
let name = "ada";
println!("{}", name.to_uppercase());
}ADA- The method comes from a trait that is not imported. Methods such as
write_all(traitstd::io::Write) orread_lineon a reader exist only when the trait is in scope: adduse std::io::Write;. The error says "items from traits can only be used if the trait is in scope" and names the trait. - The wrong type.
Option<String>has no.len(); theStringinside it does. Unwrap ormapfirst. AVec<T>has no.push_str(); that belongs toString. - A generic type that lacks a bound. A method that needs
T: Cloneis not available onFoo<T>untilTisClone. Add the bound to your function orimpl.
Common mistakes#
- Searching other languages' names (
size,length,to_upper). In Rust:len,to_uppercase,contains,push. - Ignoring the "help" lines. Most E0599 messages already contain the fix.
Run rustc --explain E0599 for more.