Created
April 30, 2014 04:53
-
-
Save tbelaire/934586fe3b2d13cac50e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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