[過去ログ]
プログラミング言語 Rust 4 (1002レス)
プログラミング言語 Rust 4 http://mevius.5ch.net/test/read.cgi/tech/1507970294/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
レス栞
このスレッドは過去ログ倉庫に格納されています。
次スレ検索
歴削→次スレ
栞削→次スレ
過去ログメニュー
60: デフォルトの名無しさん [sage] 2017/10/30(月) 22:54:07.55 ID:nDqoZaw+ ありがとう。 move するとアドレスが変わるというのは無理矢理実験したときに気付いたけれど、 実際はこんな感じで Box 化された構造体の特定の変数への参照が欲しかったので、 >>38が言った前スレの>>507-514辺りを見て、こんな感じで解決してました。 use std::mem; struct Bar(i32); struct Foo { x: Box<Bar>, x_ref: &'static i32, } impl Foo { fn new() -> Self { let mut foo = Foo { x: Box::new(Bar(10)), x_ref: unsafe { mem::uninitialized() }, }; let dummy = mem::replace(&mut foo.x_ref, unsafe { mem::transmute(&foo.x.0) } ); mem::forget(dummy); foo } } http://mevius.5ch.net/test/read.cgi/tech/1507970294/60
61: デフォルトの名無しさん [sage] 2017/10/31(火) 00:31:43.31 ID:pbuN/n26 >>60 ああ、確かにリファレンスとはいえ forget() しとくべきだね。 このコードについては↓でも問題ないけど、まあ実際の初期化はもっと他にも 処理があるだろうから、一般にはこううまくは行かなかいか。 struct Foo<'a> { x: Box<Bar>, x_ref: &'a i32, } impl<'a> Foo<'a> { fn new() -> Self { let x = Box::new(Bar(10)); Foo { x_ref: unsafe { mem::transmute(&x.0) }, x: x, } } } http://mevius.5ch.net/test/read.cgi/tech/1507970294/61
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.028s