jweinst1
4/17/2017 - 6:23 PM

Uses boxes and enums to have an infinitely long binary tree

Uses boxes and enums to have an infinitely long binary tree

//binary computation tree

//simulates infinite binary tree of pluses using boxes
enum BinComp {
    Int(i32),
    Plus(Box<BinComp>, Box<BinComp>)
}

impl BinComp {
    fn call(self) -> i32 {
        match self {
            BinComp::Int(num) => num,
            BinComp::Plus(l, r) => l.call() + r.call()
        }
    }
}

fn main() {
   let f = BinComp::Plus(Box::new(BinComp::Int(8)), Box::new(BinComp::Int(6)));
   println!("{}", f.call());
   //14
}