-
-
Save Lysxia/972d06eb7d73a1bbea71a03f0c1b00d5 to your computer and use it in GitHub Desktop.
Fun with order of evaluation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let mut q = Q(0); | |
| *{ println!("left" ); &mut q.0 } += { print! ("right "); 1 }; // right left | |
| { print! ("left "); &mut q.0 }.add_assign({ println!("right"); 1 }); // left right | |
| *{ print! ("left "); &mut q } += { println!("right"); Q(1) }; // left right | |
| { print! ("left "); &mut q }.add_assign({ println!("right"); Q(1) }); // left right | |
| ltr(0usize); // left right, even though += is used with usize: the evaluation order is determined before monomorphization | |
| fn ltr<T: AddAssign + Copy>(mut x: T) { | |
| let y = x; | |
| *{ print! ("left "); &mut x } += { println!("right"); y } | |
| } | |
| } | |
| #[derive(Clone, Copy)] | |
| struct Q(usize); | |
| use std::ops::{Add, AddAssign}; | |
| impl Add for Q { | |
| type Output = Q; | |
| fn add(self, r: Self) -> Self { | |
| Q(self.0 + r.0) | |
| } | |
| } | |
| impl AddAssign for Q { | |
| fn add_assign(&mut self, r: Self) { | |
| *self = *self + r | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment