Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2015 07:37
Show Gist options
  • Save anonymous/5044e946f822abc38154 to your computer and use it in GitHub Desktop.
Save anonymous/5044e946f822abc38154 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#[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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment