问题描述
为什么要编译这段代码?
Why does this code compile?
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]
is a local variable and iter()
borrows it. This code should not compile because the returned value holds a reference to a local variable.
推荐答案
在您的示例中, [1, 2, 3]
不被视为局部变量,而是作为静态变量!
In your example, [1, 2, 3]
is not treated as local variable, but as static one!
我们来看看这段代码:
fn foo() -> &'static [i32] {
&[1, 2, 3]
}
这有效!
前段时间,RFC 1414: Rvalue Static Promotion 合并:将 constexpr 右值提升为静态内存中的值而不是堆栈槽".这意味着基本上你写的所有文字都可以永远存在.因此,像 let _: &'static i32 = &42;
这样的东西也可以工作!
Some time ago, RFC 1414: Rvalue Static Promotion was merged: "Promote constexpr rvalues to values in static memory instead of stack slots". This means that basically all literals you write can live forever. Thus, things like let _: &'static i32 = &42;
also work!
如果我们避免使用文字数组,我们可以看到预期的错误:
If we avoid using a literal array, we can see the expected error:
fn bar() -> impl Iterator<Item = i32> {
vec![1, 2, 3].iter().map(|&i| i)
}
这里我们得到v
寿命不够长"错误.
Here we get the "v
does not live long enough" error.
这不仅限于整数或数组;它广泛适用于任何仅由文字组成的文字:
This isn't limited to integers or arrays; it applies broadly to any literal that is composed solely of literals:
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')
}
除了文字之外,这也适用于标准库中微小数量的函数,但这些可能是错误的.决定任意 const
函数的结果是否可以自动提升为 static
仍然是一个 开放主题.
Beyond literals, this also works for a tiny number of functions in the standard library, but these were likely a mistake. Deciding on if the result of arbitrary const
functions can be automatically promoted to static
is still an open topic.
这篇关于为什么我可以返回对局部文字的引用而不是变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!