SHARE
TWEET

Untitled

a guest Oct 19th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Person {
  2.     name: String,
  3.     surname: String
  4. }
  5.  
  6. // El trait define el metodo complete_name que debe ser implementado
  7. pub trait CompleteName {
  8.     fn complete_name(&self) -> String;
  9. }
  10.  
  11. //Se implementa el trait para el tipo Person
  12. impl CompleteName for Person {
  13.      fn complete_name(&self) -> String {
  14.         format!("{}: {}", self.name, self.surname)
  15.  
  16.     }
  17. }
  18.  
  19. fn main(){
  20.     let person = Person {
  21.         name: String::from("Peter"),
  22.         surname: String::from("Jackson")
  23.     };
  24.     // Ahora ya se puede llamar al metodo complete_name del tipo Person
  25.     println!("Complete name is {}", person.complete_name());
  26.     //OUTPUT Complete name is Peter: Jackson
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top