Skip to content

Instantly share code, notes, and snippets.

@Igosuki
Created April 26, 2020 12:31
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 Igosuki/754e53f111d1a9a955c1e475a136b64c to your computer and use it in GitHub Desktop.
Save Igosuki/754e53f111d1a9a955c1e475a136b64c to your computer and use it in GitHub Desktop.
Life of a tree
#[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