Why does Rust allow calling functions via null pointers?

前端 未结 5 947
臣服心动
臣服心动 2021-02-05 00:21

I was experimenting with function pointer magic in Rust and ended up with a code snippet which I have absolutely no explanation for why it compiles and even more, why it runs.

5条回答
  •  灰色年华
    2021-02-05 01:02

    Given that rust is built on top of LLVM, and that what you're doing is guaranteed UB, you're likely hitting something similar to https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html. This is one of many reasons why safe rust works to eliminate all UB.

提交回复
热议问题