(no title)
stymaar | 3 years ago
There's three levels when on the ownership ladder:
1. Taking ownership of an object: this means your have mutability and 'static lifetime. This often implies cloning though.
2. Having the object behind a ref-counted pointer (Rc or Arc in multi-trhreaded scenario) this way you have 'static but not mutability (unless you're using interior mutability with RefCell/Mutex but now you have to think about deadlock and all)
3. Taking shared references (&) to your object. It's the most performant but now you have to deal with lifetimes so it's not always worth it.
Rust beginners often jump from 1. to 3. (Or don't because “too tedious”) but 2. is a sweet spot that works in many cases and should not be overlooked!
0b01|3 years ago
willglynn|3 years ago
This is a technique I've used in anger: https://github.com/AS207960/xml-serde/pull/8