Created
April 26, 2020 12:31
-
-
Save Igosuki/754e53f111d1a9a955c1e475a136b64c to your computer and use it in GitHub Desktop.
Life of a tree
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(Debug)] | |
enum Tree { | |
Seed, | |
Sprout, | |
Seedling, | |
Sapling, | |
Mature, | |
Decline, | |
Snag, | |
} | |
fn unbox<T>(value: Box<T>) -> T { | |
*value | |
} | |
impl From<Box<Tree>> for Tree { | |
fn from(t: Box<Self>) -> Self { | |
let next = match *t { | |
Self::Seed => Self::Sprout, | |
Self::Sprout => Self::Seedling, | |
Self::Seedling => Self::Sapling, | |
Self::Sapling => Self::Mature, | |
Self::Mature => Self::Decline, | |
Self::Decline => Self::Snag, | |
t => t, | |
}; | |
next | |
} | |
} | |
fn tree_life() { | |
let mut tree = Box::new(Tree::Seed); | |
loop { | |
tree = Box::new(tree.into()); | |
println!("Tree is now a {:?}", tree); | |
if let Tree::Snag = *tree { | |
break; | |
} | |
} | |
} | |
fn main() { | |
tree_life(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment