Skip to content

rust-guide::20 method syntax - Suggestion to explain method chaining and types of self #16969

Closed
@anmej

Description

@anmej

cc @steveklabnik

In chapter 20 of the guide you write that x.foo().bar().baz() has better readability than baz(bar(foo(x))). However, afterwards you only give an example of a simple fn area(&self) -> f64 method.

I suggest adding an example of a method that allows chaining. Something like fn foo(&mut self) -> &mut T {...}, where T is the type of the struct.

In addition, it would be nice to explain the distinction between methods that take 'self', 'mut self', '&self' and '&mut self'. All of those can be used to implement method chaining, but with different purposes - some can be used to mutate the original value, while others can't, etc...

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