Created
January 5, 2018 13:32
-
-
Save Kerollmops/1acfe36111d3892b231a64b4298b975f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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