【发布时间】:2016-04-02 21:25:29
【问题描述】:
编译下面的代码(代码 1)时出现这些错误
错误:
v寿命不够长 vec.push(&v);注意:引用必须对以下语句的块后缀有效 0 点 15:64...
注意:...但是借用的值只对块后缀有效 以下是 19:35 的声明 2
(代码 1)
fn main() {
let mut vec: Vec<&Inf> = Vec::<&Inf>::new();//<-- It appears the error
let p: Foo1 = Foo1::created();
let v: Foo2 = Foo2::created();
vec.push(&v);
vec.push(&p);
但当我移动vec、p 和v 下方时不会。
(代码 2)
fn main() {
let p: Foo1 = Foo1::created();
let v: Foo2 = Foo2::created();
//It does not appear the error described above
let mut vec: Vec<&Inf> = Vec::<&Inf>::new(); //<-- It does not appear the error
vec.push(&v);
vec.push(&p);
..//
(这种行为可能很正常,如果有人可以解释一下。)
这是我创建的类似案例,因此您可以看到错误
没有错误play.rust
【问题讨论】:
标签: rust