4.5 impl and methods
Declaration in impl | Receiver |
|---|---|
fn m() | &self |
var fn m() | &var self |
move fn m() | self by value (move) |
static fn m() | none, called as T.m() |
init m() | constructor (§4.3) |
op +(…) | operator (§5.3) |
- Inside a method
selfandSelfare available. Fields and methods are available withoutself.unless the name is shadowed by a local variable. impl T { … }MUST be located in the package that declaredT.impl I for T { … }is an explicit implementation of an interface. Allowed ifIorTis declared in the current package.
Associated constants. const NAME: T = e inside impl T or interface declares a constant of the type, available as T.NAME:
impl Money {
pub const ZERO: Money = Money{ minor: 0 }
pub const MAX_TRANSFER: Money = Money.rub(1_000_000)
}
interface Codec<T> { const NAME: str } // the implementation must supply a value
impl Codec<Point> for GeoHashCodec { const NAME: str = "geohash" }eMUST be a comptime expression; calls are allowed only tocomptimefunctions and to constructors without effects.- An interface constant without a value is mandatory for an implementation; with a value — it is the default.
- Associated constants are allowed in attribute arguments (
@timeout(Limits.CHECKOUT)).
Related diagnostics
impl-foreign-type, unknown-method
See also
Source: docs/spec/oxl-standard.en.md