How it works...
The impressive benchmark results are no accident: Rc objects are smart pointers to locations on the heap, and while we still call clone to do a deep copy, Rc only duplicates a pointer and increments the number of references to it. While the actual example function is kept simple so that we don't have to worry about it, it does have all the properties of complex functions we often encounter. We define the first version, which only works with owned memory (the input parameter is not a reference), in Step 1. Steps 2 and 3 show the consequences of the API chosen in Step 1: we need to call the clone function if we want to keep (a copy of) the data we pass in.
In Steps 4 to 6, we do the equivalent with a Rust construct called ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access