Prefer
fn main() { impl Foo { pub fn frob(&self, w: widget) { ... } } }impl Foo { pub fn frob(&self, w: widget) { ... } }
over
fn main() { pub fn frob(foo: &Foo, w: widget) { ... } }pub fn frob(foo: &Foo, w: widget) { ... }
for any operation that is clearly associated with a particular type.
Methods have numerous advantages over functions:
T
" (especially when using rustdoc).self
notation, which is more concise and often more
clearly conveys ownership distinctions.[FIXME] Revisit these guidelines with UFCS and conventions developing around it.
[FIXME] We need guidelines for when to provide inherent methods on a type, versus methods through a trait or functions.
NOTE: Rules for method resolution around inherent methods are in flux, which may impact the guidelines.