3 ms·
It's been about a year since I wrote any Rust, but one of the things I struggled with is having an intuition for the "right way" of dealing with the borrow chec
by Eridrus 10y ago
It's been about a year since I wrote any Rust, but one of the things I struggled with is having an intuition for the "right way" of dealing with the borrow checker.
I often found myself storing indexes into vectors/maps and not being sure if this was a good way of doing things; it seemed better than raw pointers in some senses, but it also felt like I was just bypassing the borrow checker and using a system that had less drastic failure modes, but othewise no guarantees.
I also found myself building up datastructures of these indexes that I would not have had to do in a traditional imperative language.
I would have really appreciated some examples of writing your own data structures where a traditional encoding would run into the borrow checker (eg graphs).
- steveklabnik 10y agoThanks, totally hear that. We should promote http://cglab.ca/~abeinges/blah/too-many-lists/book/ http://cglab.ca/~abeinges/blah/too-many-lists/book/ more...
- Eridrus 10y agoI had a brief glance at this and while this seems to present a continuous story, I had to read all the way to section 2.4 to learn anything new (mem::replace) without any idea of whether I would. I would have prefered a reference book of techniques with examples to this format, though maybe starting with a ToC of techniques would be better than a rant about linked lists. [EDIT]: I read some more and this seems like really useful info... just not advertised or IMO presented well.
- caconym_ 10y agoYes! This document was instrumental for me in getting my foot in the door of understanding how to use memory in Rust.
- carols10cents 10y agoI agree with Steve that you'll probably love the "Learning Rust With Entirely Too Many Linked Lists" that he linked to. While not as complex as that, I would like to hear what you think about the slices section in the new version of the book: http://rust-lang.github.io/book/ch04-03-slices.html http://rust-lang.github.io/book/ch04-03-slices.html It introduces slices by starting with functions that use indexes into `String`s, which sounds similar to what you were doing. It demonstrates the problems with that approach, and then shows how to use slices instead. I'm not sure if slices would have worked well in your cases, but this chapter is so similar to what you said I wanted to point it out!
- scott_s 10y agoI liked the slices section. (And I personally found it more direct and more useful than the linked list post.)
- carols10cents 10y agoAwesome!!! Glad to hear it :)