4 ms·
Ok, but I repeat: what is the use case? Yes, this allows me to do things like add an item to a Vec without modifying it in place, but why would I want to do th
by winstonewert 9y ago
Ok, but I repeat: what is the use case?
Yes, this allows me to do things like add an item to a Vec without modifying it in place, but why would I want to do that? What's the situation where that's helpful?
In other languages I do that, specifically to avoid mutability hell, but that doesn't apply to rust.
EDIT: Thanks to the responders for the great examples!
- masklinn 9y ago> Yes, this allows me to do things like add an item to a Vec without modifying it in place, but why would I want to do that? What's the situation where that's helpful? The context is obviously shared concurrency (hence the library using Arc period), so you can cheaply get a shared immutable structure and manipulate it on your side (without sharing it back), and if necessary share/swap it in at some point, they provide cheap read/copy/update cycles. With regular collections Rust would preclude misuses but that would still require either deep copies upfront or fairly large exclusive critical sections.
- the8472 9y agoIt might be useful for RCU data structures in multithreaded code
- cryptonector 9y agoAbsolutely. This is one very good use case. Say you have a configuration to read and share with all threads. Each thread can make local changes that will not be visible to the others. The API looks like it's mutating the shared object, but it's actually mutating a copy.
- caconym_ 9y agoOne example: storing a large text buffer in a persistent data structure (e.g. a rope) can allow you to maintain a space-efficient history of "snapshots" simply by keeping pointers to older versions of the buffer.
- Groxx 9y agoNo ability to mutate -> no mutable ownership complications. Ever. And that's probably the biggest pain-point for people using Rust. Not necessary, but can simplify things, by encouraging(/forcing) simpler use of data by default.
- divs1210 9y agomaintain history/rewind state to any previous stable time, like git/datomic.