我有以下 Rust 模块。
test.rs:
pub fn foo(xs: &[f32]) -> Vec<f32> {
xs.iter()
.flat_map(|x| xs.iter().map(|y| *x - y))
.collect()
}
Run Code Online (Sandbox Code Playgroud)
如果我编译这个模块(作为rlib),我会得到一个错误:
$ rustc -o test.s --crate-type rlib test.rs
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
[...]
Run Code Online (Sandbox Code Playgroud)
也可以使用godbolt进行同样的实验(它的作用几乎相同)。
但是,如果我在二进制包中编译相同的函数。编译器接受此代码。
例如,通过使用rust-playground。
(请注意,我们在这里使用相同的编译器版本;即 1.63.0)
为什么编译器在这两种情况下表现不同?
我怀疑链接器可能会在这里启动,但我想得到更多的技术确认。
它与板条箱类型无关,与版本有关。2021 版改进了围绕关闭的借用检查器。
在 Rust Playground 中,如果单击构建配置文件和工具链按钮右侧的三点菜单,并选择 2018 版本,则会再次出现错误:https: //play.rust-lang.org/ ?version =稳定&模式=调试&版本=2018&要点=f8bb0c7ad9000ad9a9d4540351c74721
rustc为了向后兼容,可能默认使用最旧的 Rust 版本(2015)。Godbolt 也做同样的事情,因为它只是运行rustc。Rust Playground 使用最新版本。
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |