Description
The following code use the recently stabilized, (but not in the current stable release yet,) feature drop_types_in_const
, so it needs a recent nightly version to be compiled.
struct Foo {
bar: String,
}
static mut FOO: Option<Foo> = None;
fn main() {
unsafe {
FOO = Some(std::mem::zeroed());
}
if let Some(_) = unsafe { FOO.as_mut() } {
println!("Some(Foo)");
} else {
println!("None");
}
}
This prints out "None", instead of the expected "Some(Foo)". However, if I change FOO = Some(std::mem::zeroed());
to FOO = Some(std::mem::uninitialized());
then it prints "Some(Foo)", (at least on my system, but that could clearly be just "luck".) The assignment also works as expected if I explicitly initialize FOO
, that is, something like FOO = Some("Hello World!".to_string());
, or if I use a non-drop type. If I use a different built-in drop type than String
, like Vec<u32>
or Box<u32>
then it still prints "None". However, if I define my own stack-allocated drop type like so:
struct Baz{
qux: u32
}
impl Drop for Baz {
fn drop(&mut self) {}
}
and make bar
an zeroed instance of that type then it prints "Some(Foo)".