Can't understand the 'use of moved value' error


#1

Hello there,

I’m leaning Rust and I just encountered my first problem. I have the following snippet:

struct Variable {
    v: i32
}

impl Variable {
    fn new(v: i32) -> Variable {
        Variable {v: v}
    }

    fn feed(mut self, a: i32) -> Variable {
        self.v = a;
        self
    }

    fn output(self) -> i32 {
        let x = self.v;
        x
    }
}

fn main() {
    let a = Variable::new(4);
    println!("{}", a.output());
    println!("{}", a.feed(5).output());
}

And the compiler obviously yells at me by saying that

error[E0382]: use of moved value: `a`
  --> src/main.rs:25:20
   |
24 |     println!("{}", a.output());
   |                    - value moved here
25 |     println!("{}", a.feed(5).output());
   |                    ^ value used here after move
   |
   = note: move occurs because `a` has type `Variable`, which does not implement the `Copy` trait

I actually can’t understand why I do have this error. It seems I’m not taking ownership of a, why do I have this error? Can you introduce me to it? Thanks!


#2

Problem is with argument here.
Chapter 4 and 5 of the book should hopefully help. If there is anything specific that you find you don’t understand post back with detail.


#3

Thank you so much for the link! I will give a look!