On Sun, Feb 3, 2013 at 7:22 AM, Ziad Hatahet <[email protected]> wrote:
> Hi all,
>
> I was playing around with the different pointer types in Rust, and I have
> the following program: https://gist.github.com/4702154
>
> My questions/comments are listed in lines 16, 23, 27, and 29.
>
Line 16: The recent "move based on type" changes mean that when you
refer to a variable that has a non-implicitly-copyable type -- `c`,
which is mutable and has type `~int` in this case -- writing `c` means
`move c`. So that means if you use `c` again in a place where a
previous use of `c` may have occurred, you get an error because `c`
has already been moved out of and zeroed out.
The answer here is to either write `copy c` instead of c, or pass `c`
by reference.
Line 23: `c` isn't incremented because you passed a new pointer to a
copy of the contents of `c` in the previous call to `inc`.
Line 27: Likewise, modify doesn't do what I think you expect, for the
same reason as in Line 23. Here's some C code (that probably won't
compile), that shows the basic premise:
void inc(int *x) {
(*x)++;
}
void main() {
int y = 3;
int *z = &y;
inc(z);
printf("%d\n", *z);
int *w = malloc(sizeof(int));
*w = *z;
inc(w);
printf("%d\n", *z);
}
You wouldn't expect the second printf to print out 5, right?
Line 29: Same issue as with Line 16.
Hope this helps!
> As a side note, I believe that there is still work being done on having
> mutability depend on the containing slot, could that be a source of some of
> the issues in the code?
No, not in this case.
Cheers,
Tim
--
Tim Chevalier * http://catamorphism.org/ * Often in error, never in doubt
"Too much to carry, too much to let go
Time goes fast, learning goes slow." -- Bruce Cockburn
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev