Why this fires¶
The receiver does not have a method with the given name. Concrete causes:
- A typo (
.lenght()instead of.len()). - The method exists on a different type than the receiver is.
- The method comes from a trait that isn't in scope.
- Visibility — the method is declared but not
pub.
let s = "hello"
let n = s.lenght()
// ^^^^^^^^ error: no method 'lenght' on type 'str'Fix it¶
1. Fix the spelling¶
Trust the "did you mean?" suggestion when it appears.
2. Bring the trait into scope¶
For methods provided by a trait:
use std::iter::Iterator // brings `.map`, `.filter`, etc. into scope3. Check the receiver's actual type¶
The "I'm sure the method exists" case usually means the receiver is a different type than you expect. Add an explicit annotation or hover the value in the editor to confirm:
let parsed: Result<int, ParseError> = input.parse::<int>()
let number: int = parsed.unwrap_or(0)Strings use text.parse::<int>() or text.parse::<float>() for fallible numeric
parsing. There is no string .to_int() or .to_float() method. Concrete string
receivers are checked against the standard-library method surface; an Any
receiver remains a dynamic boundary.
4. Mark the method pub¶
If you declared the method in an impl block in another module, make sure it's pub.
See also¶
TE100— undefined variable (different lookup mechanism).- /docs/stdlib — built-in methods on
str,int,float, etc.