Last active
December 28, 2016 11:49
-
-
Save fuine/ed23181bf16ed036ff8e713f41ac8d7e to your computer and use it in GitHub Desktop.
Old lady swallowed the fly in rust
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
#[derive(PartialEq)] | |
enum Action { Once, Every, Die } | |
fn main() { | |
let animals = [ | |
("horse", Action::Die, "She's dead, of course!"), | |
("donkey", Action::Once, "It was rather wonky. To swallow a donkey."), | |
("cow", Action::Once, "I don't know how. To swallow a cow."), | |
("goat", Action::Once, "She just opened her throat. To swallow a goat."), | |
("pig", Action::Once, "Her mouth was so big. To swallow a pig."), | |
("dog", Action::Once, "What a hog. To swallow a dog."), | |
("cat", Action::Once, "Fancy that. To swallow a cat."), | |
("bird", Action::Once, "Quite absurd. To swallow a bird."), | |
("spider", Action::Once, "That wriggled and jiggled and tickled inside her."), | |
("fly", Action::Every, "I don't know why she swallowed the fly.")]; | |
for (i, a) in animals.iter().rev().enumerate() { | |
println!("I know an old lady who swallowed a {}.\n{}", a.0, a.2); | |
if a.1 == Action::Die { | |
return; | |
} | |
for (swallowed, to_catch) in | |
animals.iter().skip(animals.len() - i - 1).zip(animals.iter().skip(animals.len() - i)) { | |
println!("She swallowed the {} to catch the {}.", swallowed.0, to_catch.0); | |
if to_catch.1 == Action::Every { | |
println!("{}", to_catch.2); | |
} | |
} | |
println!("Perhaps she'll die.\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment