Advertisement
Guest User

Untitled

a guest
May 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. // Enum that works like a constructed function
  2. enum Func {
  3. Add(i32, Box<Func>),
  4. Sub(i32, Box<Func>),
  5. Nil
  6. }
  7.  
  8. impl Func {
  9. fn call(&self, value:i32) -> i32 {
  10. match self {
  11. Func::Nil => value,
  12. Func::Add(num, next) => next.call(value + num),
  13. Func::Sub(num, next) => next.call(value - num)
  14. }
  15. }
  16. }
  17.  
  18. fn main() {
  19. let foo = Func::Add(40, Box::new(Func::Sub(3, Box::new(Func::Nil))));
  20. println!("{}", foo.call(5));
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement