小编Gum*_*Gun的帖子

为什么 unsafe { x } == y 不能编译?

我在一些我认为应该有效的事情中遇到了一些编译错误:

fn function() -> bool {
    unsafe { 1 } == 1
}
Run Code Online (Sandbox Code Playgroud)
fn function() -> bool {
    unsafe { 1 } == 1
}
Run Code Online (Sandbox Code Playgroud)

我知道这有点无用,但这1确实是一个不安全的功能。但这样就可以了。

fn function() -> bool {
    let var = unsafe { 1 };
    var == 1
}
Run Code Online (Sandbox Code Playgroud)

我知道也许两者都会被优化为完全相同,但我很好奇为什么

unsafe rust

2
推荐指数
1
解决办法
85
查看次数

标签 统计

rust ×1

unsafe ×1