On 10/30/2011 01:53 PM, David Rajchenbach-Teller wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

        Dear Rusties (?),
   I am in the process of designing a small rope library for Rust and I
have a few issues wrt `str`. I am a newbie at Rust, so I may be wrong,
but it is my understanding that `str` supports a form of mutability
through an in-place append operation `+=`.
The mutability story for strings continues to make me uncomfortable. We tend to assert that strings are immutable, but they often behave as though they are mutable and they are implemented as mutable.

The reason we can consider them immutable (I think) is that they have unique ownership, so no two parties can have a view of the same string and see it mutate. When we mutate a string in Rust, what is actually happening is that the 'slot' containing the string changes to hold a (conceptually) different string. So when you do

let a = "foo";
a += "bar";

you aren't (conceptually) mutating the "foo" string to be "foobar", you are mutating the slot named 'a' to hold the string "foobar" instead of the string "foo". Because we have unique semantics for strings we actually just go ahead and mutate "foo" into "foobar". The net effect is that strings behave like scalar value types - either mutable or immutable depending on the nature of the slot they reside in. Local strings are mutable and strings inside of aggregates are only mutable when preceded with the 'mutable' keyword. What tends to happen is two parties will have a reference to the same 'slot', one will change the string in the slot and then the other party is looking at a different string.

So if you want to have a bunch of ropes looking at the same immutable string you need to copy it into a shared, immutable box. In an immutable box, you won't be able to perform append operations on it.

Someone please correct me. Obviously I spend a lot of time being confused about the situation.

Regards,
Brian

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to