Hacker News new | past | comments | ask | show | jobs | submit | invpt's comments login

I appreciate the mention of Box<[T]>. I've never (or at least very rarely?) seen people using it, even in rustc, although I'm sure there's somewhere in the compiler that uses it. I've kind of gotten the feeling that Box<[T]> is frowned upon as a needless optimization because the one-word storage difference is so minimal, but I appreciate having the immutable size semantics as a way of ensuring no extra allocations occur by way of the methods on Vec.


Another rust type you should look out for is Box<str>In my rust code I find a lot more uses of Box<str> rather than Box<[T]>. Strings are often fixed length identifiers or usernames that get passed around frequently and stored in data structures. Box<str> can be a drop-in replacement in a surprising amount of code


Arc<str> is an atomically ref-counted string, also quite useful in async or multi-threaded code where (unlike w/ Box) you sometimes can't know in advance which task will drop the last reference to some data.


Box<str> is used pervasively in the rust compiler instead of String for exactly this reason. Basically every string of code the compiler looks at is, unsurprisingly, constant.


Yeah, the general concept here is https://en.wikipedia.org/wiki/String_interning, Box<str> is great for it.


Another downside of Box<T> is that Vec::into_boxed_slice copies all elements from the source vector. Vec::shrink_to_fit does not appear to copy. This is based on the stable docs.


It doesn't copy the elements if the source vector is already at max capacity, which you can often arrange for beforehand.


For example, by explicitly calling shrink_to_fit. So you either have fragmentation, or an extra copy if you're not careful, but none of the solutions let you forget about the detail entirely.


If you look at the source for into_boxed_slice, it calls shrink_to_fit at the beginning before doing anything else. Hence the documentation is slightly wrong, and no copies occur.

Edit: I submitted a PR to clear up the docs: https://github.com/rust-lang/rust/pull/120110


thank you!


Or alternatively, the vector was created with `Vec::with_capacity()` or `ExactSizeIterator::collect()`, and had the correct capacity all along.


I asked it "what is a memory model in computer science" to see how it handles something slightly obscure. Its answer:

> The multi-store model (also known as Atkinson–Shiffrin memory model) is a memory model in computer science that describes the interactions of threads through memory and their shared use of the data. It allows a compiler to perform many important optimizations.

> More info in the History and significance section of the Memory model (programming) Wikipedia article.

It seems to think that the Atkinson–Shiffrin memory model is somehow related to computer science, which it is not. It's a model of human memory. And that article it references does not once mention the Atkinson–Shiffrin memory model. At least it's easy to verify.


Maybe it thinks linked sub-references on Wikipedia are more relevant than other information on the computer memory pages. Or maybe it just took "memory model" without the computer context.


Yeah, I think the problem is that the memory model (computer science) article is too short, so the model ended up spitting out information from something seemingly related, i.e., human memory models.


That's great to hear! When I get the chance, I'll update the article to note that custom context menus are actually supported now.


I guess I'm thinking of Flutter's desktop support in relation to how it is on mobile. Flutter mobile does have a lot of built in stuff.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: