Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. trait DuckLike {
  2. fn quack(&self);
  3.  
  4. fn walk(&self) {
  5. println!("walking");
  6. }
  7. }
  8.  
  9. struct Duck;
  10.  
  11. impl DuckLike for Duck {
  12. fn quack(&self) {
  13. println!("quack");
  14. }
  15. }
  16.  
  17. impl DuckLike for i64 {
  18. fn quack(&self) {
  19. for _ in 0..*self {
  20. println!("quack");
  21. }
  22. }
  23. }
  24.  
  25. //generics type parameters that "parameter: trait" makes boundary
  26. fn duck_go<D: DuckLike>(duck: D) {
  27. duck.quack();
  28. duck.walk();
  29. }
  30.  
  31. fn main() {
  32. let duck = Duck;
  33. let f = 0.0;
  34. duck_go(duck);
  35. //duck_go(f);
  36. //float does not implement DuckLile therefore, f is not be a parameter
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement