jweinst1
4/3/2017 - 6:07 AM

example, cannot use mutable method in rust enum

example, cannot use mutable method in rust enum

//using mutable methods on enums
//uses list case



enum Value {
    List(Vec<Value>),
    Int(i32)
}

impl Value {
    //mutable push
    fn push(self, val:Value) {
        match self {
            Value::List(mut l) => l.push(val),
            _ => ()
        }
    }
    
    fn print(self) {
        match self {
            Value::List(l) => for i in l {
                match i {
                    Value::Int(num) => println!(" {} ", num),
                    Value::List(_) => i.print()
                }
            },
            Value::Int(nm) => println!(" {} ", nm)
        }
    }
}




fn main() {
   let mut cont = Value::List(vec![Value::Int(8),Value::Int(2)]);
   cont.push(Value::Int(8));
   cont.print();
}

/*
rustc 1.16.0 (30cf806ef 2017-03-10)
error[E0382]: use of moved value: `cont`
  --> <anon>:39:4
   |
38 |    cont.push(Value::Int(8));
   |    ---- value moved here
39 |    cont.print();
   |    ^^^^ value used here after move
   |
   = note: move occurs because `cont` has type `Value`, which does not implement the `Copy` trait

error: aborting due to previous error*/