Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::Add;
- fn add<L: Add<R>, R>(l: L, r: R) -> <L as Add<R>>::Output {
- l + r
- }
- fn add_native_int<I: Add<I, Output=I>>(l: I, r: I) -> I {
- l + r
- }
- // simple identity-function
- fn f1<T>(t: T) -> T{
- t
- }
- // other function, can be used in the same way as `f1` in some expressions
- fn f2<T>(_t: T) -> Foo {
- Foo()
- }
- struct Foo();
- impl Add<u8> for Foo {
- type Output = u8;
- fn add(self, rhs: u8) -> u8 {
- rhs
- }
- }
- fn main() {
- // original expression, case 1
- let x: u8 = f1(1) + 2;
- // mutatd expression, using `add_native_int`
- let x : u8 = add_native_int(f1(1), 2);
- // mutatd expression, using `add` -> DOES NOT COMPILE
- //let x : u8 = add(f1(1), 2);
- // original expression, case 2
- // indistinguishable from the first on the AST level but different global context
- let x: u8 = f2(1) + 2;
- // mutatd expression, using `add_native_int` -> DOES NOT COMPILE
- //let x : u8 = add_native_int(f2(1), 2);
- // mutatd expression, using `add`
- let x : u8 = add(f2(1), 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement