安全 Rust 中可能存在未定义的行为吗?

Is undefined behavior possible in safe Rust?

有没有什么方法可以在不使用 unsafe 的情况下在 Rust 中实现未定义的行为?

当然,此类行为可以由第三方库包装在“安全”函数中,因此假设我们只使用标准函数。

当然可以,但是 any such case is a bug with Rust or the standard libary

我最喜欢的例子是LLVM loop optimization can make safe programs crash,它实际上是由于 Rust 和 LLVM 语义的交互不良而发生的:

pub fn oops() {
    (|| loop {
        drop(42)
    })()
}

在 Rust 1.49.0 上进行优化编译,生成程序集:

playground::oops:
    ud2

such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one

标准库一个“第三方库”,所以我没有区别。