3 ms·
> 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
by masklinn 6y 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.