Skip to content

Instantly share code, notes, and snippets.

@mh-mobile
Created December 2, 2021 03:42
Show Gist options
  • Save mh-mobile/f5364ce4694243e6ce1ee6a07a04c55d to your computer and use it in GitHub Desktop.
Save mh-mobile/f5364ce4694243e6ce1ee6a07a04c55d to your computer and use it in GitHub Desktop.
use std::io;
trait Fruit {
fn get_size(&self) -> f32;
}
struct Apple {
size: f32,
}
struct Pine {
size: f32,
}
impl Fruit for Apple {
fn get_size(&self) -> f32 {
self.size
}
}
impl Fruit for Pine {
fn get_size(&self) -> f32 {
self.size
}
}
fn main() {
let stdin = io::stdin();
let mut input = String::new();
let _ = stdin.read_line(&mut input);
let apple = Apple { size: 80.0 };
let pine = Pine { size: 100.0 };
let data = input.trim();
let fruit: &dyn Fruit = match data {
"1" => &apple,
"2" => &pine,
_ => panic!("error!"),
};
println!("size: {}", fruit.get_size());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment