3 ms·
What I appreciate coming from D is fine grained control of mutability. In Rust it is all or nothing. You cannot have a mutable container of immutable items. In
by qznc 5y ago
What I appreciate coming from D is fine grained control of mutability. In Rust it is all or nothing. You cannot have a mutable container of immutable items. In D a string as a mutable array of immutable characters, for example. You can append things but not change the existing parts.
- masklinn 5y ago> In D a string as a mutable array of immutable characters, for example. You can append things but not change the existing parts. Nothing precludes designing a Rust string that way. You're still designing the mutation interface of the collection, and having a `&mut` to a structure doesn't really give you anything if the structure doesn't allow mutations. Hell, you could actually design your own pseudo-string, which derefs to an immutable string, and only provides for a select number of mutations. But, given Rust's rules around mutation, what useful property would that yield exactly? I think that's one of the most interesting effects of Rust's strict ownership, very much an "if a tree falls" situation: if you mutate a string in place but there's no way for anyone else to observe that mutation, does it matter? I can imagine that "append-only strings" would be useful in a world of multiple-ownership where you could delay COW copies without affecting co-owners (though even that seems… somewhat limited), but that's not really a thing in Rust. Given the cow-ownership would be not one but two layers of external wrappers (an Rc/Arc pointer, and some sort of internal mutability shim), it's not like you could not flatten the entire thing and build thread-safe append-only COW pseudo-strings.
- pornel 5y agoRust can have mixed mutability - see Interior Mutability (Cell/RefCell/Atomic/Mutex). But in Rust the distinction is actually about shared vs exclusive access. This is the aspect that really matters for safety. Access is properly of the code, not data, so it can be dynamic from data's perspective (the same data can be borrowed exclusively or not at different times) When you have exclusive access, immutability becomes irrelevant, because nobody else can observe the mutation (so e.g. exclusive reference to a Mutex can skip locking!)