Why doesn't std::mem::drop undo shadowing?

fn main() {
    let a = 8u8;
    let a = Box::new(3);
    std::mem::drop(a);
    println!("{a}")
}

Compiler said:

error[E0382]: borrow of moved value: `a`

Why doesn't std::mem::drop() undo the shadowing to the 1st a?

mem::drop isn't a special compiler built-in or anything, its just a normal function. It lets you control when drop code runs for a value, but it doesn't affect the variable scoping. If you want a narrower scope for the inner a you can use a block

fn main() {
    let a = 8u8;
    {
        let a = Box::new(3);
    }
    println!("{a}")
}
9 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.