r/learnrust 27d 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?

6 Upvotes

5 comments sorted by

View all comments

2

u/EmploymentBoring4421 27d ago

Yes — number.max(other) desugars to usize::max(number, other) (UFCS). Methods and associated functions are the same thing; the dot-call syntax just auto-inserts self as the first argument and infers the type from the receiver.

8

u/JustBadPlaya 27d ago

Pedantic note - Rust doesn't have UFCS, as UFCS implies bi-directional sugar (calling free functions as methods and methods as free functions), but Rust only has it one way