Created
April 11, 2019 18:15
-
-
Save frgomes/a01b11a074eb9f7483d07d6b37a63a03 to your computer and use it in GitHub Desktop.
Rust - instanceOf
This file contains hidden or 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
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
https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=19c3e3dec1ba2a142ef6fd8c4571be8a