Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0615: attempted to take value of method on type
What it meansYou wrote a method name without parentheses, so Rust thinks you want a field with that name. Add () to call it.
What rustc prints
error[E0615]: attempted to take value of method `method` on type `Foo` --> main.rs:12:3 | 12 | f.method; // error: attempted to take value of method `method` on type `Foo` | ^^^^^^ method, not a field | help: use parentheses to call the method | 12 | f.method(); // error: attempted to take value of method `method` on type `Foo` | ++
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
v.leninstead ofv.len().- A getter method with the same name as a private field.
How to fix it
1. Call the method
let n = v.len();Other wordings of this error
attempted to take value of method on type, use parentheses to call the method
The compiler team's explanation
Attempted to access a method like a field.
Erroneous code example:
struct Foo {
x: u32,
}
impl Foo {
fn method(&self) {}
}
let f = Foo { x: 0 };
f.method; // error: attempted to take value of method `method` on type `Foo`
If you want to use a method, add () after it:
f.method();
However, if you wanted to access a field of a struct check that the field name is spelled correctly. Example:
println!("{}", f.x);From rustc --explain E0615 (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
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0610`u32` is a primitive type and therefore doesn't have fields
- E0614type cannot be dereferenced
- E0617can't pass `f32` to variadic function
- E0620cast to unsized type: `&[usize; 2]` as `[usize]`
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?