Closed
Description
For a simple sample check this file: https://gist.github.com/MarkJr94/5888147
This is the Character trait:
pub trait Character {
fn x(&self)-> int;
fn y(&self)-> int;
fn set_x(&mut self, int);
fn set_y(&mut self, int);
fn translate(&mut self, dx: int, dy: int);
fn name(&self) -> ~str;
fn new(x: int, y:int) -> Self;
fn rest(&mut self);
fn as_str(&self) -> ~str;
fn hp(&self) -> uint;
fn hp_mut<'r> (&'r mut self) -> &'r mut uint;
fn stamina(&self) -> uint;
fn attack(&mut self, player: &mut Character) -> uint;
}
The issue is specific to Trait objects. Changing player: &mut Character
to a player: &mut Mummy
works.
Moreover, there is a different issue, in that this problem is also solved by changing player: &mut Character
to mut player: &mut Character
which should not be necessary, as I do not reassign the player reference/pointer.
Minimal Example:
// traitfail.rs
trait Slide {
fn new(x: int) -> Self;
fn x_mut<'r> (&'r mut self) -> &'r mut int;
fn x(&self) -> int;
}
struct Bead {
x: int
}
impl Slide for Bead {
pub fn new(x: int) -> Bead {
Bead { x: x}
}
pub fn x(&self) -> int {
self.x
}
pub fn x_mut<'r>(&'r mut self) -> &'r mut int {
&'r mut self.x
}
}
fn main() {
let slide = &mut Slide::new::<Bead>(5) as &mut Slide;
let x = slide.x();
let x_mut = slide.x_mut();
*x_mut += 2;
}
// Compiler output
traitfail.rs:30:13: 30:18 error: cannot borrow immutable local variable as mutable
traitfail.rs:30 let x_mut = slide.x_mut();
^~~~~
error: aborting due to previous error