Skip to content

Instantly share code, notes, and snippets.

@eddyb
Forked from drguildo/main.rs
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyb/d9b541cfdd318db1b6f2 to your computer and use it in GitHub Desktop.
Save eddyb/d9b541cfdd318db1b6f2 to your computer and use it in GitHub Desktop.
mod simple;
use simple::Expr::{Number, Add, Multiply};
fn main() {
println!("Let's compute!");
let e = Add(Box::new(Add(Box::new(Number(1)),
Box::new(Number(1)))),
Box::new(Multiply(Box::new(Number(2)),
Box::new(Number(4)))));
println!("{}", e);
}
use std::fmt;
pub enum Expr {
Number(i32),
Add(Box<Expr>, Box<Expr>),
Multiply(Box<Expr>, Box<Expr>),
}
impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expr::Number(n) => write!(f, "{}", n),
Expr::Add(ref l, ref r) => write!(f, "({} + {})", l, r),
Expr::Multiply(ref l, ref r) => write!(f, "({} * {})", l, r),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment