|
|
|
|
|
by zootm
3697 days ago
|
|
If you have a string slice (&str) you need to convert it to a String you own before you can mutate it, yes. In some other languages strings are always immutable, and mutating them requires creating a copy. In Rust if you own the memory (and haven't given out any immutable references to anyone), you can mutate it. String literals are an interesting case for the reason mentioned in the earlier comment: they reference memory that no code "owns", as in-place mutation of the executable would be unsafe. Some languages have mutable strings but these are usually unsafe if used concurrently, or require locks. In Rust this is modeled in the type/borrow system. |
|
Those languages used to introduce mutable wrappers and not a "to_string()" method.
> they reference memory that no code "owns", as in-place mutation of the executable would be unsafe.
Is that similar to the dynamics of uniqueness types?