Why is it considered safe to `mem::forget` Boxes?
07:43 16 Dec 2022

The following code creates boxes, each pointing to a block of 4096 bytes. If I run this on release, everything is optimised away :(, but on debug, this behaves as expected and leaks a bunch of memory... or does it?

fn main() {
    for _ in 0..1000 {
        for _ in 0..100000 {
            let b = Box::new([!0u64; 1 << 10]);
            std::mem::forget(b);
        }
        let mut buf = String::new();
        let _ = std::io::stdin().read_line(&mut buf);
    }
}

Why is this not considered unsafe?

rust unsafe