Advertisement
Guest User

Untitled

a guest
May 26th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #![allow(unused_variables, dead_code)]
  2.  
  3. pub trait Summary {
  4. fn summarize(&self) -> String;
  5. }
  6.  
  7. pub struct Tweet {
  8. pub username: String,
  9. pub content: String,
  10. pub reply: bool,
  11. pub retweet: bool,
  12. }
  13.  
  14. impl Summary for Tweet {
  15. fn summarize(&self) -> String {
  16. format!("{}: {}", self.username, self.content)
  17. }
  18. }
  19.  
  20. // Use trait bounds to say what functionality the param must implement.
  21. fn notify<T: Summary>(item: T) {
  22. println!("Breaking news! {}", item.summarize());
  23. }
  24.  
  25. fn main() {
  26. let tweet = Tweet {
  27. username: String::from("horse_ebooks"),
  28. content: String::from("of course, as you probably already know, people"),
  29. reply: false,
  30. retweet: false,
  31. };
  32.  
  33. notify(tweet);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement