Created
October 11, 2019 18:15
-
-
Save jcavat/0e4599b3919f32764669143c733e99a0 to your computer and use it in GitHub Desktop.
Pattern match on traits 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
struct Foo{ value: u32 } | |
struct Bar{ value: u32 } | |
trait Base<T> { | |
fn accept(&self, v: &dyn Visitor<Result = T>) -> T ; | |
} | |
impl <T>Base<T> for Foo { | |
fn accept(&self, v: &dyn Visitor<Result = T>) -> T { | |
v.visit_foo(&self) | |
} | |
} | |
impl <T>Base<T> for Bar { | |
fn accept(&self, v: &dyn Visitor<Result = T>) -> T { | |
v.visit_bar(&self) | |
} | |
} | |
trait Visitor { | |
type Result; | |
fn visit_foo(&self, foo: &Foo) -> Self::Result; | |
fn visit_bar(&self, bar: &Bar) -> Self::Result; | |
} | |
struct StringVisitor {} | |
impl Visitor for StringVisitor { | |
type Result = String; | |
fn visit_foo(&self, foo: &Foo) -> String { | |
format!("it was Foo: {:}!", foo.value) | |
} | |
fn visit_bar(&self, bar: &Bar) -> String { | |
format!("it was Bar: {:}!", bar.value) | |
} | |
} | |
fn test<T>(v: bool) -> Box<dyn Base<T>> { | |
if v { | |
Box::new(Foo{value: 5}) | |
} else { | |
Box::new(Bar{value: 10}) | |
} | |
} | |
fn main() { | |
let f = test(true); | |
println!("{:}", f.accept( &StringVisitor{} )); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to https://stackoverflow.com/a/58346517/3012181