In functions you will have to deal with the choice of passing a reference or owned data. Usually the choice is made by the function itself:

If your code must own data, make the caller provide owned data

There are cases when you don’t know until runtime, such as in return types to represent functions that sometimes allocate. For this case, Cow can be used, which lets you represent data that may be owned by holding either a reference or an owned value. Cow means Clone-On-Write and it only clones when mutation is needed.

In general, if you cannot reference due to lifetime issues, first clone inexpensive data then heap allocate huge chunk of bytes if it is performance sensitive.

See also

References

  • J. Gjengset, Rust for Rustaceans: Idiomatic Programming for Experienced Developers. No Starch Press, 2021.