4 ms·
I've been writing Rust code since before the 1.0 days, and I still can't understand lifetimes in practice. When the compiler starts complaining about lifetimes
by jedisct1 6y ago
I've been writing Rust code since before the 1.0 days, and I still can't understand lifetimes in practice.
When the compiler starts complaining about lifetimes issues, I tend to make everything clone()able (either using Rc, or Arc, or Arc+Mutex, or full clones).
Because if you start introducing explicit lifetimes somewhere, these changes are going to cascade, and tons of annotations will need to be added to everything using these types, and their dependent types.
- saagarjha 6y agoI think you're really intended to do the latter rather than the former. I mean, Rust lets you do either–it gives you the choice if performance isn't your concern–but usually it's better to not clone everything.
- steveklabnik 6y agoNah, either is fine. Rust gives you the tools to do both for good reason. Which is right for you completely depends.
- glandium 6y agoI find that lifetimes are ok, albeit annoying sometimes, especially the cascade part, as you mention. The one thing I can't get to stick in my brain is variance. Every time, I need to go back to https://doc.rust-lang.org/nomicon/subtyping.html#variance https://doc.rust-lang.org/nomicon/subtyping.html#variance