[rust-dev] Two mutable pointers to same memory address?
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, grayfox ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Two mutable pointers to same memory address?
This is sound- you aren't actually making more than one mutable reference here. Stepping through the operations in your example: 1. i is a mutable pointer to an immutable memory location 2. Pass a pointer to i into `bar`, which can mutate `i`. 3. Deref that pointer so you have a copy of `i`, which still points to an immutable memory location. If you try to assign through `i` in `bar`, you'll quickly see that the compiler won't let you. Here's an example in the playpen [1] which fails to compile with cannot assign to immutable dereference of ``-pointer. If you were doing this in C, the difference between `let bar = mut foo` and `let mut bar: T = mut foo` is equivalent to the difference between `const *` and `const *const`. The former can be changed to point somewhere else, but neither of them allows writing into the pointed-to memory. The real trick here is that when we specify the type of `bar` as `T` we opt out of mutability- if you remove the type from that assignment, it's inferred to be `mut T`. [1] http://is.gd/FqaGiL On Wed, Nov 26, 2014 at 10:26 AM, grayfox gray...@outerhaven.de 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, grayfox ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev -- Peter Marheine Don't Panic ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Two mutable pointers to same memory address?
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, grayfox I 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 Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev