Does the indirection of a pointer immediately create a reference?

⚓ Rust    📅 2026-07-03    👤 surdeus    👁️ 1      

surdeus

Consider this example:

struct Wrapper<T> {
    live: bool,
    data: T,
}
fn main() {
    let ptr = Box::into_raw(Box::new(Wrapper {
        live: true,
        data: 0i32,
    }));
    unsafe {
        std::ptr::drop_in_place(&mut (*ptr).data); // #0
        _ = *ptr; // #1
       // _ = & mut *ptr; // #2
    }
}

Does #1 create a reference? Another question is: does #2 cause UB? If yes, where is the relevant rule? I know a relevant rule is

A reference or Box<T> must be aligned and non-null, it cannot be dangling, and it must point to a valid value (in case of dynamically sized types, using the actual dynamic type of the pointee as determined by the metadata). Note that the last point (about pointing to a valid value) remains a subject of some debate.

However, I don't find the rule that says data has invalid value after #0.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed