Casting from NonNull (how does it work?)

⚓ Rust    📅 2026-05-13    👤 surdeus    👁️ 1      

surdeus

  struct MyType;
  let n: NonNull<()> = NonNull::from(&MyType).cast(); 
  let _m = unsafe { n.cast::<MyType>().as_ref() };

in this example I've created a pointer that initially points at MyType but then I've made it point to a unit...What does it even mean for it to point at a unit, does it mean that it points nowhere?
Also, when I cast it back to MyType; is it pointing at a completely new MyType or is it the MyType which we had before casting it to a unit? How does casting even work when there is no type information available at runtime....? thanks!

3 posts - 3 participants

Read full topic

🏷️ Rust_feed