Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0599: no method named X found in the current scope
What it meansThe method does not exist on that type, or it comes from a trait that is not imported, or its trait bounds are not met.
What rustc prints
error[E0599]: no method named `chocolate` found for struct `Mouth` in the current scope
--> main.rs:6:3
|
3 | struct Mouth;
| ------------ method `chocolate` not found for this struct
...
6 | x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
| ^^^^^^^^^ method not found in `Mouth`Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
Why it happens
- Trait methods (like
read_to_stringfromstd::io::Read, or.write_all) need the trait in scope. - Calling a method on
Option<T>orResult<T, E>that belongs toT; unwrap or match first. - Calling
.iter()methods on something that is not an iterator, or a method from a newer crate version than the one you have. - A derive is missing, so a method that needs
CloneorOrdis unavailable.
How to fix it
1. Import the trait
The compiler often names it in a help line.
use std::io::Read;
let mut s = String::new();
file.read_to_string(&mut s)?;2. Unwrap the Option or Result first
The method is on the inner type.
let len = maybe_name.as_ref().map(|n| n.len()).unwrap_or(0);Other wordings of this error
no method named X found in the current scope, no method named found, method not found in, no function or associated item named, items from traits can only be used if the trait is in scope, the method exists but the following trait bounds were not satisfied
The compiler team's explanation
This error occurs when a method is used on a type which doesn't implement it:
Erroneous code example:
struct Mouth;
let x = Mouth;
x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
// in the current scope
In this case, you need to implement the chocolate method to fix the error:
struct Mouth;
impl Mouth {
fn chocolate(&self) { // We implement the `chocolate` method here.
println!("Hmmm! I love chocolate!");
}
}
let x = Mouth;
x.chocolate(); // ok!From rustc --explain E0599 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0308mismatched types
- E0277the trait bound is not satisfied
- E0433failed to resolve
- E0282type annotations needed
- E0588packed type cannot transitively contain a `#[repr(align)]` type
- E0593closure is expected to take 0 arguments, but it takes 1 argument
- E0600cannot apply unary operator `!` to type `Question`
- E0604only `u8` can be cast as `char`, not `u32`
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?