Skip to content

Instantly share code, notes, and snippets.

@DaGenix
Last active December 18, 2015 13:59
Show Gist options
  • Save DaGenix/5794340 to your computer and use it in GitHub Desktop.
Save DaGenix/5794340 to your computer and use it in GitHub Desktop.
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