3 ms·
Honestly, 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 buil
by juancampa 5y ago
Honestly, 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.