I ran into an error:
extern crate rustc_serialize; // 0.3.24
use rustc_serialize::base64::{self, FromBase64, ToBase64};
fn main() {
let a: [u8; 30] = [0; 30];
let b = a.from_base64().unwrap().as_slice();
println!("{:?}", b);
}
The error:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:7:13
|
7 | let b = a.from_base64().unwrap().as_slice();
| ^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
8 | println!("{:?}", b);
9 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
For me, the code can do no wrong, though. Why am I having that error?
let a: [u8, ..30] = [ 123, 34, ... ];
can be onerous as you have to adapt the length whenever you change the array. If you wish to document the length, this is fine, if you do not need it and feel the pain though, there is another way to express this:let a = [123u8, 34, ...];
. The key is that123
and34
are just generic integrals (no specific type), so you are constraining them by specifyingu8
; however, because arrays are homogenous containers, another solution is to constrain the type of one (the first, typically, so123u8
) and the compiler deduces that all areu8
! – Matthieu M.