3 ms·
In Rust (end even C++ afaik) it usually makes more sense to let the callee decide. Does it need to own the value? -> Call by value. Does it only need to inspect
by dthul 7y ago
In Rust (end even C++ afaik) it usually makes more sense to let the callee decide. Does it need to own the value? -> Call by value. Does it only need to inspect or (visibly) mutate the value during the function call? -> Call by reference (modulo optimization concerns).
- danieldk 7y agoFunctions can also be generic over references vs. move/copy semantics, e.g.: fn foo<T>(frob: T) where T: Borrow<Frob> { // Do something with frob.borrow() } Due to monomorpization, foo is specialized for moves/clones when called as foo(frob) where frob is a value type or for borrows when called as foo(&frob). Similarly, you can use Clone, ToOwned or even Into for going in the opposite direction. Whether such functions are good taste in general is debatable ;).
- Arnavion 7y agoYou want AsRef, not Borrow, unless you need the additional guarantees that Borrow provides (identical comparisons and hashing).