FAQ - Help and questions

How do I fix "no method named ... found" (E0599)?

#errors#compiler#methodspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

E0599 means the type has no method with that name in scope. Check the spelling first, since the compiler suggests similar names. Then check that the method exists for this type, that its trait is imported with use, and that a generic type satisfies the trait bounds the method needs.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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#

  1. A typo or a name from another language. The compiler lists similar names:
src/main.rsrust
fn main() {
    let name = "ada";
    println!("{}", name.to_upper());
}
text
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`.
  1. Use the suggested name. Rust calls it to_uppercase:
src/main.rsrust
fn main() {
    let name = "ada";
    println!("{}", name.to_uppercase());
}
text
ADA
  1. The method comes from a trait that is not imported. Methods such as write_all (trait std::io::Write) or read_line on a reader exist only when the trait is in scope: add use std::io::Write;. The error says "items from traits can only be used if the trait is in scope" and names the trait.
  2. The wrong type. Option<String> has no .len(); the String inside it does. Unwrap or map first. A Vec<T> has no .push_str(); that belongs to String.
  3. A generic type that lacks a bound. A method that needs T: Clone is not available on Foo<T> until T is Clone. Add the bound to your function or impl.

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.

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet