Created
June 9, 2021 08:56
-
-
Save Steboss89/e6a2086e317009cab022ab9868d1f063 to your computer and use it in GitHub Desktop.
Independent implementation of a trait
This file contains hidden or 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
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