我是 Rust 新手,我对借用检查器的行为感到非常困惑。
trait Foo {
fn foo(&self);
}
struct Bar<'a> {
pub f : &'a Vec<i32>
}
impl<'a> Foo for Bar<'a> {
fn foo(&self) {
for i in self.f {
println!("{}", i);
}
}
}
fn call(b : &Box<dyn Foo>) {
b.foo();
}
fn main() {
let a = vec!(1,2,3);
let b : Box<dyn Foo> = Box::new(Bar {f : &a});
call(&b)
}
Run Code Online (Sandbox Code Playgroud)
通过编译这段代码我得到:
error[E0597]: `a` does not live long enough
--> main.rs:23:44
|
23 | let b : Box<dyn …Run Code Online (Sandbox Code Playgroud) rust ×1