Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. use std::ops::Add;
  2.  
  3. fn add<L: Add<R>, R>(l: L, r: R) -> <L as Add<R>>::Output {
  4. l + r
  5. }
  6.  
  7. fn add_native_int<I: Add<I, Output=I>>(l: I, r: I) -> I {
  8. l + r
  9. }
  10.  
  11. // simple identity-function
  12. fn f1<T>(t: T) -> T{
  13. t
  14. }
  15.  
  16. // other function, can be used in the same way as `f1` in some expressions
  17. fn f2<T>(_t: T) -> Foo {
  18. Foo()
  19. }
  20.  
  21. struct Foo();
  22. impl Add<u8> for Foo {
  23. type Output = u8;
  24. fn add(self, rhs: u8) -> u8 {
  25. rhs
  26. }
  27. }
  28.  
  29. fn main() {
  30. // original expression, case 1
  31. let x: u8 = f1(1) + 2;
  32.  
  33. // mutatd expression, using `add_native_int`
  34. let x : u8 = add_native_int(f1(1), 2);
  35. // mutatd expression, using `add` -> DOES NOT COMPILE
  36. //let x : u8 = add(f1(1), 2);
  37.  
  38. // original expression, case 2
  39. // indistinguishable from the first on the AST level but different global context
  40. let x: u8 = f2(1) + 2;
  41.  
  42.  
  43. // mutatd expression, using `add_native_int` -> DOES NOT COMPILE
  44. //let x : u8 = add_native_int(f2(1), 2);
  45. // mutatd expression, using `add`
  46. let x : u8 = add(f2(1), 2);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement