Skip to content

Instantly share code, notes, and snippets.

@sebastianfrelle
Last active August 22, 2019 11:11
Show Gist options
  • Save sebastianfrelle/d9b20981388cd599b3ce481bda4cea17 to your computer and use it in GitHub Desktop.
Save sebastianfrelle/d9b20981388cd599b3ce481bda4cea17 to your computer and use it in GitHub Desktop.
Greeting between two people
trait Greetable {
fn greeting_for_other(&self, other: &Self) -> String;
}
struct Person<'a> {
name: &'a str,
age: u8,
}
impl<'a> Greetable for Person<'a> {
fn greeting_for_other(&self, other: &Self) -> String {
return format!(
"Hi, {}; I'm {}, and I'm {} years old!",
other.name, self.name, self.age
);
}
}
fn main() {
let p1 = Person {
name: "Sebastian",
age: 25,
};
let p2 = Person {
name: "Simon",
age: 24,
};
let s = p1.greeting_for_other(&p2);
println!("{}", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment