lang::impl¶
Inherent and trait implementation blocks.
An inherent impl Type block adds methods and associated functions; an
impl Trait for Type block supplies a trait's methods.
struct Point { x: i64, y: i64 }
impl Point {
fn origin() -> Point { Point { x: 0, y: 0 } } // associated fn
fn norm(&self) -> i64 { self.x * self.x + self.y * self.y }
}
let p = Point::origin() // qualified path always resolves
println!("{}", p.norm())
A method takes self, &self, or &mut self; &self reads, &mut self
writes through to the caller's storage. Qualified-path calls
(Point::origin()) always resolve; method-call dispatch is type-directed for
user impls, core collection/String receivers, and typed stdlib receivers.
Prefer the qualified form when several types intentionally share an associated
function name and you want the call target to be explicit.
Generic impls¶
Methods on a generic struct use the impl<T> form, and each receiver type
specialises the method (so -> T returns the real instantiated type):
Operator and conversion impls¶
Operator overloading (impl Add for T, impl Index for T, ...) and
conversions (impl T { fn from(...) }, fn try_from(...)) are ordinary
impls - see trait for the operator/method table and the
into / try_into inference rules. The comparison (eq / cmp) and
clone behaviour is synthesized with no impl; write an eq / cmp impl
only to override the default ordering.