Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. trait T1 {
  2. fn f1(&self) -> i32;
  3. }
  4.  
  5. trait T2 {
  6. fn f2(&self) -> f32;
  7. }
  8.  
  9. struct S1 {
  10. id: i32,
  11. }
  12.  
  13. struct S2 {
  14. iid: i32,
  15. fid: f32,
  16. }
  17.  
  18. impl T1 for S1 {
  19. fn f1(&self) -> i32 { self.id }
  20. }
  21.  
  22. impl T1 for S2 {
  23. fn f1(&self) -> i32 { self.iid }
  24. }
  25.  
  26. impl T2 for S2 {
  27. fn f2(&self) -> f32 { self.fid }
  28. }
  29.  
  30. // wish: fn test<T: T1 - T2>
  31. fn test<T: T1>(val: T) {
  32. println!("test<T: T1> = {}", val.f1());
  33. }
  34.  
  35. // wish: fn test<T: T1 + T2>
  36. fn test2<T: T1 + T2>(val: T) {
  37. println!("test<T: T1 + T2> = {}, {}", val.f1(), val.f2());
  38. }
  39.  
  40. fn main() {
  41. let s1 = S1 { id: 32 };
  42. let s2 = S2 { iid: 64, fid: 128.0 };
  43.  
  44. test(s1);
  45. test2(s2); // wish: test(s2);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement