jweinst1
4/2/2017 - 11:59 PM

reduces a vector in rs

reduces a vector in rs

//example of struct with methods in Rust
//meant to simulate stack machine


struct IntStack {
    items:Vec<i32>
}

impl IntStack {
    //constructor function for IntStack
    fn new() -> IntStack {
        IntStack{items:vec![]}
    }
    
    fn push(&mut self, num: i32) {
        self.items.push(num);
    }
    
    fn reduc_add(&mut self) -> i32 {
        while self.items.len() > 2 {
            self.items[0] += self.items[self.items.len()-1];
            self.items.pop();
        }
        self.items[0]
    }
}


fn main() {
   let mut a = IntStack::new();
   for i in 0..10 {
       a.push(i);
   }
   println!("The total is {}", a.reduc_add());
}