Rust slice definition as reference with and without & -
Rust slice definition as reference with and without & -
i have next sample:
extern crate debug; utilize std::mem::size_of_val; struct a<'a> { a: &'a [i64], } fn main() { // code }
when define piece &
programme print in console:
println!("{} - `{:?}`", size_of_val(&a { a: &[1, 2, 3] }), { a: &[1, 2, 3] }); // 16 - `a<'static>{a: &[1i64, 2i64, 3i64]}`
defining piece without &
give same result:
println!("{} - `{:?}`", size_of_val(&a { a: [1, 2, 3] }), { a: [1, 2, 3] }); // 16 - `a<'static>{a: &[1i64, 2i64, 3i64]}`
when seek move definition &
variable steel work:
let x = { a: &[1, 2, 3] }; println!("{} - `{:?}`", size_of_val(&x), x); // 16 - `a<'static>{a: &[1i64, 2i64, 3i64]}`
but when seek move definition without &
variable:
let x = { a: [1, 2, 3] }; println!("{} - `{:?}`", size_of_val(&x), x);
i have next build error:
/prpath/main.rs:12:20: 12:29 error: borrowed value not live long plenty /prpath/main.rs:12 allow x = { a: [1 ,2, 3] }; ^~~~~~~~~ /prpath/main.rs:11:11: 15:2 note: reference must valid block @ 11:10... /prpath/main.rs:11 fn main() { /prpath/main.rs:12 allow x = { a: [1 ,2, 3] }; /prpath/main.rs:13 /prpath/main.rs:14 println!("{} - `{:?}`", size_of_val(&x), x); /prpath/main.rs:15 } /prpath/main.rs:12:5: 12:31 note: ...but borrowed value valid statement @ 12:4; consider using `let` binding increment lifetime /prpath/main.rs:12 allow x = { a: [1 ,2, 3] }; ^~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due previous error
i expect a { a: &[1, 2, 3] }
definition allowed because a.a
should have &[i64]
type, rust allow skip &
alter behaviour variable. difference between a { a: &[1, 2, 3] }
, a { a: [1, 2, 3] }
definitions or why a { a: [1, 2, 3] }
definition allowed ?
first, can utilize [t,..n]
&[t]
expected, conversion piece implicit. next code valid:
let = [1u, 2, 3]; allow b: &[uint] = a;
your situation purely lifetime problem. struct
struct a<'a> { a: &'a [i64], }
it holds slice. piece nil more reference first element , count of number of elements. that's why size_of_val()
called on a
homecoming 16: it's size of slice, 1 u64 pointer, , 1 u64 number of elements (as seems on 64bits computer).
so in code, the struct not own array. difference in behavior observe due difference when array out of scope.
first case:
let x = { a: [1, 2, 3] };
here define array, , stores piece array in struct. when reaching ;
, array out of scope , destroyed, , reference in x
not valid anymore: forbidden compiler.
second case:
let x = { a: &[1, 2, 3] };
it more peculiar. array stored in anonymous variable. in fact, writing
let foo = &42u;
is equivalent writing
let _anonymousvariable = 42u; allow foo = &_anonymousvariable;
except can't reach _anonymousvariable
directly.
it's same you, code equivalent to
let _anonymous_array = [1, 2, 3] allow x = { a: &_anonymous_array };
and valid.
last case:
when write straight in println!()
. previous case, see why works:
println!("{} - `{:?}`", size_of_val(&a { a: &[1, 2, 3] }), { a: &[1, 2, 3] });
but in case there no problem either:
println!("{} - `{:?}`", size_of_val(&a { a: [1, 2, 3] }), { a: [1, 2, 3] });
because arrays go out of scope when reaching ;
, , no reference them exist past point, can safely deleted , compiler happy.
rust
Comments
Post a Comment