gpt4 book ai didi

rust - 在安全的 Rust 中是否可能出现未定义的行为?

转载 作者:行者123 更新时间:2023-12-03 11:25:31 25 4
gpt4 key购买 nike

有没有办法在不使用 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


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

关于rust - 在安全的 Rust 中是否可能出现未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62559509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com