Skip to content

Instantly share code, notes, and snippets.

@frgomes
Created April 11, 2019 18:15
Show Gist options
  • Save frgomes/a01b11a074eb9f7483d07d6b37a63a03 to your computer and use it in GitHub Desktop.
Save frgomes/a01b11a074eb9f7483d07d6b37a63a03 to your computer and use it in GitHub Desktop.
Rust - instanceOf
enum FooBar {
Foo(u8),
Bar(u8)
}
fn test(v: bool) -> FooBar {
if v {
FooBar::Foo(5)
} else {
FooBar::Bar(10)
}
}
fn main() {
let f: FooBar = test(true);
// Now that we have a `Box<Base>` (`*f` makes it a `Base`),
// let's handle different cases:
match f {
FooBar::Foo(x) => println!("it was Foo: {}!", x),
FooBar::Bar(y) => println!("it was Bar: {}!", y),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment