Over the weekend, I was working on a personal Rust project when I ran into an excessive memory usage problem. After an evening of trial and error, I found a workaround to fix the memory usage, but I still didn’t understand how the issue was even possible, so I then spent another evening digging through the source code of the Rust standard library to understand the root cause.
I just skimmed it but this seems to be the practical lesson for coders:
I actually came in here to post just that. It’s something that, while I’ve vaguely known, is not something I typically remember to do - I’m usually returning or collecting Vecs all over. I shall be now diligent & intentional from now on.
For shared, immutable lists, you can also use
Arc
/Rc
in place ofBox
! I guess what’s surprising is that[T]
(andstr
/Path
/OsStr
/etc) are traditionally associated with shared borrows (like&[T]
), but they’re really just unsized types that can be used anywhere there’s a?Sized
bound.Also, overuse of lists (as opposed to other data structures) seems to be a common problem in all programming languages among less experienced devs since they’re simple enough to use in most places. Not saying that’s what happened to the author though - these types are way less obvious than something like a hash set or queue.
Does
Box<[T]>
have the relevant trait implementations to be used in all the places where people misuseVec
?