When writing APIs, it's common to have a mutable and an immutable version of a method.
I expected the standard library to have clear conventions about how to name these, but it's not totally consistent1:
What are good naming conventions for the following methods?
pub fn foo****(&self) -> &Bar { ... }
pub fn foo****(&mut self) -> &mut Bar { ... }
foo() | foo_mut()
This seems the most common, and can be seen in Vec.iter and Vec.iter_mut.
foo_ref() | foo_mut()
Used for Any.downcast_ref and Any.downcast_mut.
It seems the first case is more common, so what are the reasons for using the _ref suffix when naming API functions?
1: It is probably consistent and I'm just failing to notice the reasoning.
Yes, conventions for this are defined in RFC 199. The important part is:
The rules
Immutably borrowed by default
If
foouses/produces an immutable borrow by default, use:
- The
_mutsuffix (e.g.foo_mut) for the mutably borrowed variant.- The
_movesuffix (e.g.foo_move) for the owned variant.However, in the case of iterators, the moving variant can also be understood as an
intoconversion,into_iter, andfor x in v.into_iter()reads arguably better thanfor x in v.iter_move(), so the convention isinto_iter.NOTE: This convention covers only the method names for iterators, not the names of the iterator types. That will be the subject of a follow up RFC.
Owned by default
If
foouses/produces owned data by default, use:
- The
_refsuffix (e.g.foo_ref) for the immutably borrowed variant.- The
_mutsuffix (e.g.foo_mut) for the mutably borrowed variant.
Any::downcast_ref isn't called downcast because there is a method named downcast on Box<Any + 'static> and on Box<Any + 'static + Send> that takes self by value. Naming the method on Any downcast would cause one to shadow the other. So the whole picture is:
downcast, takes self, defined on Box<Any + 'static> and Box<Any + 'static + Send>
downcast_ref, takes &self, defined on Any + 'static and Any + 'static + Send
downcast_mut, takes &mut self, defined on Any + 'static and Any + 'static + Send
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With