Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.62 KB | None | 0 0
  1. pub struct Base {
  2.     name: String,
  3. }
  4.  
  5. pub enum Fish {
  6.     FishA(Base),
  7.     FishB(Base),
  8. }
  9.  
  10. impl FIsh {
  11.     pub fn foo(&self) -> () {
  12.         match *self {
  13.             Fish::FishA(Base {ref name}) => self.fooA(),
  14.             Fish::FishB(Base {ref nom}) => self.fooB(),
  15.         }
  16.  
  17.     }
  18.  
  19.     fn fooA(&self) -> () {
  20.         println!("fooA !");
  21.     }
  22.  
  23.     fn fooB(&self) -> () {
  24.         println!("fooB !");
  25.     }
  26. }
  27. fn main() {
  28.     let v = vec![
  29.         Fish::FishA(Base { name: "John".to_owned() }),
  30.         Fish::FishB(Base { name: "Jean".to_owned() }),
  31.     ];
  32.  
  33.     for x in v {
  34.         x.foo();
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement