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!