// Test型が作成される時に使われるnew()を実装 impl Test { fn new(n: isize) -> Self { let new = Test(Box::new(n)); // その時にヒープで確保されたアドレスを表示 println!("{:p} = Test::new({n})", new.0); new } }
// Test型の足し算を実装 impl std::ops::Add for Test { type Output = Self; fn add(self, rhs: Self) -> Self::Output { Test::new(*self.0 + *rhs.0) } }
// 足し算の途中で使われる一時領域のアドレスはどうなるか? fn main() { let a = Test::new(1); let b = Test::new(10); let c = Test::new(100); let d = Test::new(1000); let e = Test::new(10000); println!("{:?}", a + b + c + d + e); }