On 26/11/14 12:26 PM, grayfox wrote:
> Hey guys,
>
> I'm really new to Rust (actually I've looked on Rust the last 5 hours the
> first time) but I think I produced something that shouldn't be possible. From
> the pointer guide I know that the following code will not compile because in
> the end I would have two mutable pointers to the same address:
>
> let x = 5i;
> let y = &x;
> let z = &x;
>
> But in the following snippet I end up with two mutable pointer tmp and *i
> which point both to the same address:
>
> fn bar<'a>(i: &mut &'a int) {
> let mut tmp = *i;
> println!("{} {}", *tmp, **i);
> }
>
> fn foo<'a>() {
> let mut i: &int = &mut 5;
> bar(&mut i);
> }
>
> fn main() {
> foo();
> }
>
> Maybe I don't understand the concept of the Rust memory concept enough but if
> I understand everything correct so far this shouldn't compile but it does
> actually.
>
> Kind regards,
>
> grayfoxI see two immutable refs being created from a mutable one, not two aliasing mutable refs. The type of `tmp` is `&int`, not `&mut int`. The fact that the variable is mutable just means that another immutable pointer can be assigned to it - it's unnecessary, as is the `mut` on `i` in `foo`.
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
