2ちゃんねる ■掲示板に戻る■ 全部 1- 最新50    

Rustアンチスレ

144 :デフォルトの名無しさん:2022/05/23(月) 09:11:41 ID:n2ZPTBPD.net
// ヒープを使う型Testを作って実証実験
#[derive(Debug)]
struct Test(Box<isize>);

// 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);
}

65 KB
新着レスの表示

掲示板に戻る 全部 前100 次100 最新50
名前: E-mail (省略可) :

read.cgi ver.24052200