Inherent methods
impl Type attaches inherent methods. A method's first parameter is self: Type; call it with receiver syntax and supply only the remaining arguments.
Methods may be public, generic, or async. Receiver selection also works through a managed pointer without exposing a raw address.
struct Point { x: int, y: int }
impl Point {
fn move(self: Point, dx: int, dy: int) -> Point {
Point { x: self.x + dx, y: self.y + dy }
}
}
fn main() {
let point = Point { x: 1, y: 2 }.move(3, 4)
println("${point.x},${point.y}")
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: 4,6
Ctrl/⌘+Enter runs the current lesson.