Associated Functions and Multiple impl Blocks
Associated functions are defined in impl but take no self — they are
called on the type itself, not on an instance. The standard idiom is to use
new (or any descriptive name) as the constructor, called with :: instead of
.:
Point::new(x, y) and Point::origin() are associated functions; distance is an instance method.
// Feature: Associated functions (no `self`) — constructors and helpers
// Syntax: define `fn name(...)` WITHOUT `self` in the impl; call
// via `Type::name(...)` with `::`
// When to use: idiomatic "constructor" and utility functions that
// don't need an instance.
struct Point {
x: float,
y: float,
}
impl Point {
// Conventional `new` constructor.
fn new(x: float, y: float) -> Point {
return Point { x: x, y: y }
}
// Another associated function — origin.
fn origin() -> Point {
return Point { x: 0.0, y: 0.0 }
}
fn distance(self, other: Point) -> float {
let dx = self.x - other.x
let dy = self.y - other.y
return (dx * dx + dy * dy) ** 0.5
}
}
// Call with `::` — note the difference from instance method (`.`).
let p = Point::new(3.0, 4.0)
let o = Point::origin()
print(p.distance(o)) // 5
// expected:
// 5
A type can have more than one impl block. All blocks are additive: the
type sees every method defined in any of them. This lets you organize methods
by responsibility — constructors in one block, geometric operations in another:
Two impl Vec2 blocks: the first defines constructors, the second math operations.
// Feature: Multiple `impl` blocks for the same type
// Syntax: several `impl Type { ... }` add up methods
// When to use: organize methods by category (constructors,
// queries, mutations, conversions) or split across logical files.
struct Vec2 {
x: float,
y: float,
}
// Block 1: constructors and utility methods.
impl Vec2 {
fn new(x: float, y: float) -> Vec2 {
return Vec2 { x: x, y: y }
}
fn zero() -> Vec2 {
return Vec2 { x: 0.0, y: 0.0 }
}
}
// Block 2: geometric operations.
impl Vec2 {
fn length(self) -> float {
return (self.x * self.x + self.y * self.y) ** 0.5
}
fn dot(self, other: Vec2) -> float {
return self.x * other.x + self.y * other.y
}
}
let v = Vec2::new(3.0, 4.0)
let z = Vec2::zero()
print(v.length()) // 5
print(v.dot(v)) // 25
print(z.length()) // 0
// expected:
// 5
// 25
// 0
Challenge
Add a third impl Vec2 block with a method normalized(self) -> Vec2 that
divides each component by the vector's length(). Test it with
Vec2::new(3.0, 4.0).normalized().
See also