On Thu, May 02, 2013 at 01:25:43AM +0200, Gábor Lehel wrote:
> Yep, that's what I was figuring. I'm not sure what "lvalue context" means
> precisely -- it's not actually on the left hand side of anything here, and
> the other example below where it *is* on the LHS is illegal -- but the
> shape of things matches what I was expecting.

Lvalues refer to values that have addresses. So by "lvalue context" I
mean a context where you evaluate the expression to determine the
address where it resides.

> > *A further aside:* don't be misled by my use of the term
> > "lvalue context" into thinking that a program like this would
> > be legal:
> >
> >     let mut str1: ~str = ~"Hello";
> >     *str1 = "World"; // ERROR
> >
> > This is illegal because assigning to an lvalue of unsized type is
> > illegal, even though `*str1` appears in an lvalue context.
> >
> 
> Hmm. If I'm thinking right this is because the size of the string is stored
> in the pointer, which, if the string gets changed behind its back, would
> become invalid?

The main reason for this is that there is no guarantee that the length
of the string you are writing in matches the length of the string you
are overwriting.  In the example above, `str1` is allocated to a
string of 5 characters---so what happens if you do `*str1 = "a very
long string"`?  Memory overruns...?


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

Reply via email to