An implementation attaches methods to a type or satisfies a trait contract.
Trait implementations
Use impl Trait for Type.
trait Summary {
fn summary(self): string
}
struct Article {
title: string
}
impl Summary for Article {
fn summary(self): string {
return self.title
}
}The checker verifies method names, receiver shape, parameter types, return types, and required associated items.
Completeness
Every required method must be implemented. A method with a trait default can be omitted.
trait Named {
fn name(self): string
fn is_empty(self): bool {
return self.name().is_empty()
}
}An implementation may override a default with a compatible method.
Defining a method that does not belong to the trait is an error in a trait implementation. Put additional methods in an inherent implementation.
Inherent implementations
impl Type adds methods without a trait.
impl Article {
fn is_draft(self): bool {
return self.title.is_empty()
}
}Inherent methods can also live inside the struct declaration or use a receiver-prefix function.
fn Article.word_count(self): int {
return self.title.split(" ").len()
}These forms share static member-call dispatch.
Generics
An implementation can target a generic type.
struct Box[T] {
value: T
}
impl[T] Box[T] {
fn get(self): T => self.value
}It can also be a blanket trait implementation constrained by bounds.
trait Printable: Display + Debug {}
impl[T] Printable for T
where T: Display + Debug {}The body is checked with the declared parameters and requirements before concrete specialization.
Coherence
Only one applicable implementation may be selected for a trait/type combination.
The compiler diagnoses:
- duplicate concrete implementations;
- overlapping unconditional blanket implementations;
- generic implementations whose constraints do not prove disjointness;
- unknown traits;
- missing required methods;
- methods not declared by the trait.
These rules keep static dispatch deterministic.
Supertraits
Implementing a trait also requires its supertrait obligations.
trait Ordered: Equatable {
fn compare(self, other: Self): int
}An Ordered implementation is usable only when the target also satisfies
Equatable.
Associated types
Bind an associated type with type Name = Type.
trait Source {
type Item
fn next(mut self): Option[Item]
}
impl Source for Counter {
type Item = int
fn next(mut self): Option[int] {
// ...
}
}The binding participates in method type checking and generic dispatch.
Associated constants use the corresponding constant binding form when the trait declares one.
Sealed traits
A trait marked @sealed can be implemented only in its declaring file.
@sealed
trait RuntimeHandle {
fn raw(self): u32
}This is a semantic restriction, not merely a lint.
Derivation
@derive(Trait, ...) asks the compiler to synthesize supported
implementations for a struct or enum.
@derive(Equatable, Hashable)
struct Key {
namespace: string
id: int
}Derived behavior must obey the same trait contract as a handwritten implementation. Unsupported traits or invalid fields are diagnosed.
Method selection
Member calls prefer an applicable inherent or trait method based on the receiver’s static type and the bounds in scope.
text := article.summary()There is no runtime search through an open method table. If method selection is ambiguous or no required implementation exists, compilation fails.