New Paste

 

Recent Pastes

Administrate




Pastebin on pastebin.tropi.us

 
PasteID: wd
Pasted by Valiant Coder Skarmory, 02:40:35 1441334435GMTC o September 04, 2015 GMT
Expires Never
Paste size 600 b
Toggle Expand   Wrap   Style   Raw
 
  1.   
  2. trait SaysHello {  
  3.     fn get_greeting(&self) -> &str;  
  4. }  
  5.   
  6. struct ClassicHello;  
  7.   
  8. impl SaysHello for ClassicHello {  
  9.     fn get_greeting(&self) -> &str {  
  10.         "Hello, World!"  
  11.     }  
  12. }  
  13.   
  14. impl SaysHello for u8 {  
  15.     fn get_greeting(&self) -> &str {  
  16.         match self {  
  17.             &42 => "Hello, Arthur Dent.",  
  18.             _ => "Hello, number I don't know how to format yet."  
  19.         }  
  20.     }  
  21. }  
  22.   
  23. fn say_hello<T: SaysHello>(greeter: T) {  
  24.     println!("{}", greeter.get_greeting());  
  25. }  
  26.   
  27. fn main() {  
  28.     say_hello(ClassicHello);  
  29.     say_hello(42);  
  30.     say_hello(0);  
  31. }  
  32.  

 
 

 
 
 
 
 
Written by Knoxious.co.uk, 2010.