Methods

Methods are functions defined inside a struct block that can be called on instances of that struct.

struct_def = [ directives ] [ "pub" ] "struct" IDENT "{" [ field_list ] [ method_list ] "}" ;
field_list = field_def { "," field_def } [ "," ] ;
method_list = method_def { method_def } ;
method_def = [ directives ] "fn" IDENT "(" [ method_params ] ")" [ "->" type ] block ;
method_params = method_param { "," method_param } [ "," ] ;
method_param = "self" | ( IDENT ":" type ) ;

Method Definition

A method is a function defined inside a struct block that takes self as its first parameter.

The self parameter represents the receiver value and has the type of the enclosing struct.

struct Point {
    x: i32,
    y: i32,

    fn get_x(self) -> i32 {
        self.x
    }
}

fn main() -> i32 {
    let p = Point { x: 42, y: 10 };
    p.get_x()  // Returns 42
}

Method Calls

Methods are called using dot notation: receiver.method(args).

A method call receiver.method(args) is desugared to a function call with the receiver as the first argument.

Methods MAY have additional parameters after self.

struct Point {
    x: i32,
    y: i32,

    fn add(self, dx: i32, dy: i32) -> Point {
        Point { x: self.x + dx, y: self.y + dy }
    }
}

fn main() -> i32 {
    let p = Point { x: 10, y: 20 };
    let p2 = p.add(32, 0);
    p2.x  // Returns 42
}

Method Chaining

When a method returns the same struct type, method calls MAY be chained.

struct Counter {
    value: i32,

    fn inc(self) -> Counter {
        Counter { value: self.value + 1 }
    }
}

fn main() -> i32 {
    let c = Counter { value: 39 };
    c.inc().inc().inc().value  // Returns 42
}

Associated Functions

A function in a struct block that does not take self as its first parameter is an associated function.

Associated functions are called using path notation: Type::function(args).

struct Point {
    x: i32,
    y: i32,

    fn origin() -> Point {
        Point { x: 0, y: 0 }
    }
}

fn main() -> i32 {
    let p = Point::origin();
    p.x  // Returns 0
}

Multiple Methods

A struct may have multiple methods defined in its block.

Method names MUST be unique within a struct definition.

struct Point {
    x: i32,
    y: i32,

    fn get_x(self) -> i32 { self.x }

    fn get_y(self) -> i32 { self.y }
}

fn main() -> i32 {
    let p = Point { x: 42, y: 10 };
    p.get_x()  // Returns 42
}

Error Conditions

Calling a method on a non-struct type is a compile-time error.

Calling an undefined method is a compile-time error.

Calling an associated function with method call syntax (receiver.function()) is a compile-time error.

Calling a method with associated function syntax (Type::method()) is a compile-time error.