What Is the Difference Between Methods and Associated Functions in Rust?

Methods operate on instances using `self`, while associated functions operate on the type itself without requiring an instance.

Methods require an instance of a type to call and take self as their first parameter, while associated functions are defined on the type itself, do not take self, and are called using the Type::function() syntax.

struct Point {
    x: f64,
    y: f64,
}

impl Point {
    // Method: takes `self` and operates on an instance
    fn distance_from_origin(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }

    // Associated function: no `self`, operates on the type
    fn new(x: f64, y: f64) -> Point {
        Point { x, y }
    }
}

fn main() {
    let p = Point::new(3.0, 4.0); // Call associated function
    let d = p.distance_from_origin(); // Call method on instance
}