Last active
December 18, 2015 13:59
-
-
Save DaGenix/5794340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Works { | |
val: int | |
} | |
impl Works { | |
fn fn1(&mut self) { | |
// This seems to work perfectly fine | |
self.fn2(&const self.val); | |
} | |
fn fn2(&mut self, x: &const int) { | |
self.val += *x; | |
} | |
} | |
struct Doesnt { | |
val: ~int | |
} | |
impl Doesnt { | |
fn fn1(&mut self) { | |
// XXX - On the next line, I get a compiler error: | |
// cannot borrow `*(*self).val` as const because it is also borrowed as mutable | |
self.fn2(self.val); // Is there some way to borrow self.val this as a &const? | |
} | |
fn fn2(&mut self, x: &const int) { | |
*self.val += *x; | |
} | |
} | |
fn main() { | |
// Works fine | |
let mut x = Works { val: 5 }; | |
x.fn1(); | |
println(fmt!("val: %d", x.val)); | |
// Doesn't compile | |
let mut y = Doesnt { val: ~5 }; | |
y.fn1(); | |
println(fmt!("val: %d", *y.val)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment