Skip to main content

impl blocks and associated functions

Behaviour lives in an `impl` block, not inside the struct definition. A method takes some form of `self`; an associated function (like `new`) does not, and is called with `Type::name()`.

The receiver you choose is an API decision. `&self` reads, `&mut self` modifies, and `self` consumes the value — useful for builders and for conversions that should not leave the original around.

Rust has no constructors as a language feature. `new` is a convention, nothing more, and you can have as many named constructors as make sense.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust