You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letmut x = None::<&str>;const{assert!(
size_of::<&str>() == size_of::<Option<&str>>() &&
align_of::<&str>() == align_of::<Option<&str>>())};unsafe{// because Option contains a &str and their sizes are equal, the field offset is zerolet ptr = &mut x as*mutOption<_>as*mut&str;// use ptr::write if T needs drop// rationale: &str has a niche at null, and since `&mut Option<T>` can produce `&mut T`// the option must contain `T` exactly, given their sizes are the same there cannot be // hidden padding/discriminant bytes, and the `None` value must use a value invalid for `&str`*ptr = "hello world";}assert_eq!(x,Some("hello world"));
Currently, I can only imagine the comment above to be a logical proof and not a guarantee.