Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2015 07:37

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 28, 2015.
    41 changes: 41 additions & 0 deletions playground.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #[derive(Clone, Copy)]
    struct Comp;
    struct USB;
    struct Card;

    impl USB {
    fn get_version(&self) -> Option<f64> {
    Some(1.2)
    }
    }

    impl Card {
    fn get_usb(&self) -> Option<USB> {
    Some(USB)
    }
    }

    impl Comp {
    fn get_soundcard(&self, number: i32) -> Option<Card> {
    match number {
    0 => Some(Card),
    _ => None,
    }
    }
    }

    fn get_version(computer: Comp, card_number: i32) -> Option<f64> {
    computer.get_soundcard(card_number)
    .and_then(|card| card.get_usb())
    .and_then(|usb| usb.get_version())
    }

    fn main() {
    let computer = Comp;

    let version = get_version(computer, 0);
    println!("{:?}", version);

    let version = get_version(computer, 1);
    println!("{:?}", version);
    }