Rust Reference ยท Keywords and Statements
The impl keyword
Reference for Rust impl: inherent impl blocks, implementing traits, generic and blanket impls, the orphan rule, and impl Trait in argument and return position.
impl attaches behavior to a type. An inherent impl Type { ... } block adds associated functions and methods that belong to the type itself. A trait impl Trait for Type { ... } block says how the type fulfills a trait.
The same keyword also appears in types: impl Trait in argument position means "some type that implements this trait", and in return position it means "one specific type that I do not want to name".
impl Point { fn norm(&self) -> f64 { (self.x * self.x + self.y * self.y).sqrt() } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
impl<generics> Type<args> where bounds { items } impl<generics> Trait<args> for Type<args> where bounds { items } unsafe impl Trait for Type { ... } impl !Trait for Type {} // negative impls: unstable fn f(x: impl Trait) {} // argument position fn g() -> impl Trait { ... } // return position
| Part | Description |
|---|---|
| Items | Functions (with or without self), associated consts, and in trait impls the associated types. |
self forms |
self, &self, &mut self, self: Box<Self>, self: Rc<Self>, self: Pin<&mut Self> and similar receivers. |
| Trait impls | Must provide every required item that has no default. Provided methods may be overridden. |
Usage notes
- Many inherent blocks are allowed. A type may have several
implblocks, even in different modules of the same crate. They are useful for separating generic from specialized methods. - Orphan rule. You may implement a trait only if the trait or the type is local to your crate. You cannot implement
DisplayforVec<i32>yourself; wrap it in a newtype. - Coherence. Two impls of the same trait must never overlap. Blanket impls like
impl<T: Display> Shout for Tmake that check strict. - Impl-specific methods are possible by constraining generics:
impl<T: Copy + Into<f64>> Wrapper<T>only offersas_f64for suchT. impl Traitin arguments is shorthand for an anonymous generic parameter, so callers cannot pass turbofish arguments for it explicitly. In return position all code paths must return the same concrete type; useBox<dyn Trait>for several types.- Lifetimes are declared on the impl:
impl<'a> Parser<'a> { ... }. In 2024 edition, return-positionimpl Traitcaptures all in-scope generics and lifetimes by default; opt out withuse<..>. Selfinside an impl names the type being implemented; seeSelf.