Associated Functions

Associated functions are similar to methods in that they are also defined in the context of a struct or enum, but they do not use any of the data in the struct and as a result do not take self as a parameter.

Associated functions could be standalone functions, but they are included in a specific type for organizational or semantic reasons.

Constructors

A distinguished family of associated functions of a specific type are type constructors. Constructors are associated functions that construct, or in other words instantiate, new instances of a type. Their return type always includes the type itself, and is often just the type itself.

Public structs that have private fields must provide a public constructor, or otherwise cannot be instantiated outside of the module in which they are declared.

Declaration

In this example we will take a look at a struct; however, an enum will work in the same way.

struct Foo {
    bar: u64,
}

We start by using the impl (implementation) keyword, followed by the name of our struct, to define a function that belongs to our object i.e. a method.

impl Foo {
    // this is an associated function because it does not take `self` as a parameter
    // it is also a constructor because it instantiates
    // and returns a new instance of `Foo`
    fn new(number: u64) -> Self {
        Self { bar: number }
    }
}

Usage

To call an associated function on a type we use the following syntax.

    let foo = Foo::new(42);