0
編譯下面的代碼時,我得到這些錯誤(代碼1)錯誤:借用值僅爲參考...必須是有效的爲
error:
v
does not live long enough vec.push(&v);note: reference must be valid for the block suffix following statement 0 at 15:64...
note: ...but borrowed value is only valid for the block suffix following statement 2 at 19:35
(代碼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