为什么要编译此代码?
fn get_iter() -> impl Iterator<Item = i32> {
[1, 2, 3].iter().map(|&i| i)
}
fn main() {
let _it = get_iter();
}
[1, 2, 3]
是局部变量,iter()
借用它。该代码不应编译,因为返回的值包含对局部变量的引用。
最佳答案
在您的示例中,[1, 2, 3]
不被视为局部变量,而是静态变量!
让我们看一下这段代码:
fn foo() -> &'static [i32] {
&[1, 2, 3]
}
这行得通!
前一段时间,RFC 1414: Rvalue Static Promotion被合并:“将constexpr rvalues提升为静态内存中的值,而不是堆栈插槽中的值”。这意味着基本上您编写的所有文字都可以永远存在。因此,像
let _: &'static i32 = &42;
这样的东西也可以工作!如果我们避免使用文字数组,则可以看到预期的错误:
fn bar() -> impl Iterator<Item = i32> {
vec![1, 2, 3].iter().map(|&i| i)
}
在这里,我们收到“
v
的生命周期不足”错误。这不限于整数或数组。它广泛适用于仅由文字组成的任何文字:
fn promote_integer() -> &'static i32 {
&42
}
fn promote_float() -> &'static f64 {
&42.42
}
fn promote_str() -> &'static str {
"Hello World!"
}
struct Foo(char);
fn promote_struct() -> &'static Foo {
&Foo('x')
}
除文字外,它还适用于标准库but these were likely a mistake中的极少数函数。决定是否可以将任意
const
函数的结果自动提升为static
仍然是open topic。
关于reference - 为什么我可以返回对本地文字的引用,而不是对变量的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64565502/