Skip to content

Instantly share code, notes, and snippets.

@tbelaire
Created April 30, 2014 04:53
Show Gist options
  • Save tbelaire/934586fe3b2d13cac50e to your computer and use it in GitHub Desktop.
Save tbelaire/934586fe3b2d13cac50e to your computer and use it in GitHub Desktop.
use std::io;
#[deriving(Show)]
enum List {
Cons(~str, ~List),
Nil
}
fn print_list( lst : &List ) {
match lst {
&Cons( ref x, ref xs ) => {
println!( "Node: '{}'", x );
print_list( *xs );
}
&Nil => { println!( "Nil" ) }
}
}
fn main() {
let mut head : List = Nil;
for line in io::stdin().lines() {
let input = line.unwrap();
println!("You typed: '{}'", input.trim());
let words : ~[&str] = input.words().collect();
match words.as_slice() {
["show"] => {
print_list(&head);
println!("{}", &head);
}
["push", x] => {
println!("Pushing '{}'", x);
head = Cons( x.to_owned(), box head);
}
["pop"] => {
println!("Popping")
match head {
Cons( x, ~xs ) => {
head = xs;
println!("Popped an '{}'", x);
}
Nil => {
println!("Can't pop an empty list");
}
}
}
["exit"] => {
println!("Bye!");
break;
}
_ => println!("I don't understand")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment