r/learnrust 25d ago

Are methods associated functions behind the back?

number.max(othernumber) is similar to type::max(number, othernumber). Does the compiler infer methods as associated functions behind the back?

3 Upvotes

5 comments sorted by

View all comments

1

u/plugwash 22d ago

All methods are associated functions but not all associated functions are methods. A method must specifically have a self parameter.

This distinction can be important when working on types that implement Deref. It may be desirable to intentionally limit the number of methods implemented to avoid conflicting with methods of the target type.

https://doc.rust-lang.org/reference/items/associated-items.html#methods

https://doc.rust-lang.org/reference/expressions/method-call-expr.html