4 ms·
Lifetimes! That's what I need help with! How do you learn lifetimes?
by dicroce 5y ago
Lifetimes! That's what I need help with! How do you learn lifetimes?
- juancampa 5y agoHonestly, the best way to learn is by diving in and implementing something. Personally I learned how lifetimes work by implementing a zero-copy parser: it builds an AST that borrows from the original string instead of copying tokens out. This is one of those tasks where Rust really shines.
- spoiler 5y agoOut of curiosity, what did you parse? Since in I imagine copying things is quite useful sometimes when generating an AST. On top of that, I think sometimes you can't avoiding copying (imagine a string with special backslash sequences like \x20, you probably copy parts of the string that don't need special interpolation)
- juancampa 5y agoThat's right. At that point I learned about std::borrow::Cow (copy-on-write) and it worked pretty nicely. The unescaping is done lazily (on access) so it's indeed zero-copy unless strictly necessary. The parser was for a tiny language we use to express references within graphs over at membrane.io. similar to URLs but typed and not ambiguous. It's simple enough. Perhaps writing a URL parser would be good for learning.
- brundolf 5y agoI've linked this on HN four or five times but it remains a great introduction to the specific quirks of working with Rust: https://rust-unofficial.github.io/too-many-lists/ https://rust-unofficial.github.io/too-many-lists/
- adamnemecek 5y agoAre you aware of this? https://doc.rust-lang.org/nomicon/lifetimes.html https://doc.rust-lang.org/nomicon/lifetimes.html
- ibraheemdev 5y agoLike all of his videos, Jon Gjengset's live coding session on lifetimes is excellent: https://www.youtube.com/watch?v=rAl-9HwD858 https://www.youtube.com/watch?v=rAl-9HwD858
- hogFeast 5y agoI am building my first "big" project in Rust, and went through this issue. Lifetimes are actually pretty simple, although anonymous lifetimes and the compiler inserting implicit lifetimes at compile-time can make the actual behaviour hard to parse. But if you are saying you don't get it, and you are in the middle of a project (my state a week ago) then the issue is your code. References everywhere gets messy very quickly with Rust's borrow checking, and lifetimes should be used sparingly. I will also give my very imprecise definition: adding a lifetime to a struct means those references in the struct cannot die before the struct dies itself (and where this can go wrong is having some kind of mutable state composed of references...because when you try to mutate that state from an implementation, the data will be destroyed as you go out of scope, and you have a dangling pointer). They seem like an unnecessary complication, they certainly did to me when I was wading through compiler errors...then I try to do something wrong, and I realised why they exist. It is really worth sticking with.
- IshKebab 5y agoYou should read about it a bit first, but the only real way is to do some coding.