
159
3
장
주요 개념
해당 항목이 중간에 드롭되지 않는다고 보장할 수 없기 때문이다.
오류가 발생하는 코드
{
let boxed = Box::new(Item { contents: 12 });
let r: &'static Item = &boxed;
println!("'static item is {:?}", r);
}
error[E0597]: `boxed` does not live long enough
--> src/main.rs:344:32
|
343 | let boxed = Box::new(Item { contents: 12 });
| ----- binding `boxed` declared here
344 | let r: &'static Item = &boxed;
| ------------- ^^^^^^ borrowed value does not live long enough
| |
| type annotation requires that `boxed` is borrowed for `'static`
345 | println!("'static item is {:?}", r);
346 | }
| - `boxed` dropped here while still borrowed
그런데
Box
::
leak
함수를 이용하면 소유한
Box
<
T
>
를
T
에 대한 가변 레퍼런스로 ...