Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Created January 5, 2018 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kerollmops/1acfe36111d3892b231a64b4298b975f to your computer and use it in GitHub Desktop.
Save Kerollmops/1acfe36111d3892b231a64b4298b975f to your computer and use it in GitHub Desktop.
#[derive(Debug)]
struct Person {
name: String,
age: u32,
}
impl Person {
fn new(name: String, age: u32) -> Self {
// return Person { name: name, age: age };
Self { name, age }
}
}
#[derive(Debug)]
struct Kitchen {
person: Option<Person>,
}
impl Kitchen {
pub fn empty() -> Self {
Self { person: None }
}
pub fn new(person: Person) -> Self {
Self {
person: Some(person)
}
}
}
#[derive(Debug)]
enum Ingredient {
Apple,
Tomato,
Acorn,
Lettuce,
Grape,
}
#[derive(Debug)]
enum CookedIngredient {
CookedApple,
CookedTomato,
CookedAcorn,
CookedLettuce,
CookedGrape,
}
impl Ingredient {
fn into(self) -> CookedIngredient {
use self::Ingredient::*;
use self::CookedIngredient::*;
match self {
Apple => CookedApple,
Tomato => CookedTomato,
Acorn => CookedAcorn,
Lettuce => CookedLettuce,
Grape => CookedGrape,
}
}
}
#[derive(Debug)]
struct Casserole;
impl Casserole {
fn cook(ingredients: Vec<Ingredient>) -> Vec<CookedIngredient> {
ingredients.into_iter().map(|i| i.into()).collect()
}
}
fn main() {
let kiki = Person::new("kiki".to_string(), 12);
println!("Hello, {:?}!", kiki);
let kitchen = Kitchen::new(kiki);
println!("My Kitchen, {:?}!", kitchen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment