Skip to content

Instantly share code, notes, and snippets.

@Steboss89
Created June 9, 2021 08:56
Show Gist options
  • Save Steboss89/e6a2086e317009cab022ab9868d1f063 to your computer and use it in GitHub Desktop.
Save Steboss89/e6a2086e317009cab022ab9868d1f063 to your computer and use it in GitHub Desktop.
Independent implementation of a trait
struct Person {
name: String, // NB there's a comma here
surname: String,
age: u32
}
// Define a trait with the functionalities we want to use with a Person
trait PersonSpec {
fn compute_year_of_birth(&self) -> u32;
}
// implement the trait for Person
impl PersonSpec for Person{
fn compute_year_of_birth(&self) -> u32{
return 2021 - self.age;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment