← Back to Rust Course | Chapter 7: Structs | Lesson 4 of 7

Associated Functions

An associated function belongs to a struct's toolbox but doesn't need an actual struct instance to run -- like a factory that builds new ones.

Defining an Associated Function

An associated function lives inside an impl block just like a method, but does not take self, meaning it is not called on an existing instance.

Example: Defining an Associated Function

markup
struct Point {
    x: i32,
    y: i32,
}

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

fn main() {
    let p = Point::origin();
    println!("({}, {})", p.x, p.y);
}

The new Convention

By strong community convention, a struct's primary constructor-like associated function is named new, even though Rust has no special built-in new keyword.

Example: The new Convention

markup
struct Rectangle {
    width: f64,
    height: f64,
}

impl Rectangle {
    fn new(width: f64, height: f64) -> Rectangle {
        Rectangle { width, height }
    }
}

fn main() {
    let rect = Rectangle::new(4.0, 5.0);
    println!("{} x {}", rect.width, rect.height);
}

Calling with Type::function() Syntax

Because associated functions have no self, they are called using the type name and double colon, Type::function_name(), rather than dot syntax on an instance.

Example: Calling with Type::function() Syntax

markup
struct Square {
    side: f64,
}

impl Square {
    fn unit() -> Square {
        Square { side: 1.0 }
    }
}

fn main() {
    let sq = Square::unit();
    println!("Unit square side: {}", sq.side);
}

Multiple Associated Constructors

A struct can define several different associated functions that each construct an instance in a different way, giving callers convenient, clearly-named alternatives.

Example: Multiple Associated Constructors

markup
struct Color {
    r: u8,
    g: u8,
    b: u8,
}

impl Color {
    fn black() -> Color {
        Color { r: 0, g: 0, b: 0 }
    }
    fn white() -> Color {
        Color { r: 255, g: 255, b: 255 }
    }
}

fn main() {
    let bg = Color::white();
    println!("Background: ({}, {}, {})", bg.r, bg.g, bg.b);
}
Common Mistakes
  1. Trying to call an associated function with dot syntax like instance.new() instead of the correct Type::new() syntax.
  2. Forgetting that associated functions do not take self at all, unlike regular methods.
  3. Not following the community convention of naming a struct's simple constructor function new.
Chapter Summary
  • An associated function is defined inside an impl block but does not take self as a parameter.
  • Associated functions are called using Type::function_name() syntax, not dot syntax.
  • The convention new() is used for a struct's primary constructor-like associated function, though it is not a special keyword.
  • Associated functions are often used as constructors that return a new instance of the struct.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.