Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active August 20, 2016 09:58
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 KodrAus/1dcb8a6769cf0c427459eda3fa06c716 to your computer and use it in GitHub Desktop.
Save KodrAus/1dcb8a6769cf0c427459eda3fa06c716 to your computer and use it in GitHub Desktop.
Rust Traits
struct Person {
id: i32,
title: String
}
impl Person {
fn new(id: i32, title: &str) -> Person {
Person {
id: id,
title: title.to_string()
}
}
}
trait HasId {
fn id(&self) -> i32;
}
trait HasTitle {
fn title(&self) -> &str;
}
impl HasId for Person {
fn id(&self) -> i32 {
self.id
}
}
impl HasTitle for Person {
fn title(&self) -> &str {
&self.title
}
}
fn main() {
let person = Person::new(1, "Jan");
println!("{}: {}", person.id(), person.title());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment