Skip to content

doc: method-syntax.md: method chaining example doesn't show how to return self #25803

@FluffyLlemon

Description

@FluffyLlemon

In the method-syntax.md file:

This is called ‘method chaining’, and we can do it by returning self.

The example (copied below) that immediately follows this does not actually contain code returning self. Instead, a new Circle struct is built and returned.

The wording suggests that the next example is going to show how returning self can be done, but the example doesn't show that.

struct Circle {
    x: f64,
    y: f64,
    radius: f64,
}

impl Circle {
    fn area(&self) -> f64 {
        std::f64::consts::PI * (self.radius * self.radius)
    }

    fn grow(&self, increment: f64) -> Circle {
        Circle { x: self.x, y: self.y, radius: self.radius + increment }
    }
}

fn main() {
    let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
    println!("{}", c.area());

    let d = c.grow(2.0).area();
    println!("{}", d);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions