【发布时间】:2021-03-13 02:59:53
【问题描述】:
我在尝试回答 this question 时遇到了这个谜题。
如何/为什么编译:
#[derive(Debug,Default)]
struct Foo {
i: i32
}
#[derive(Debug)]
struct Bar<'a> {
foo: &'a Foo
}
impl<'a> Default for Bar<'a> {
fn default() -> Self {
Bar {
foo: &Foo{ i: 25 }
}
}
}
fn main() {
let bar : Bar = Default::default();
println!("{:?}", bar);
}
我们创建一个Bar 的实例,其中包含对Foo 的引用,但谁拥有Foo?它是在 default() 函数中匿名创建的,所以我期待一个错误,指出对它的引用超过了它的范围。
如果我更改 default() 实现,则会收到预期的错误:
fn default() -> Self {
let foo = Foo{ i: 25 };
Bar {
foo: &foo
}
}
error[E0515]: cannot return value referencing local variable `foo`
--> src/main.rs:15:9
|
15 | / Bar {
16 | | foo: &foo
| | ---- `foo` is borrowed here
17 | | }
| |_________^ returns a value referencing data owned by the current function
我阅读了参考文献中与 temporary scopes 相关的部分 - 我没有发现临时范围可能超出函数范围的任何情况。
【问题讨论】:
标签: rust borrow-checker