“borrowed value does not live long enough” when using as_slice()

后端 未结 1 1171
灰色年华
灰色年华 2020-12-11 15:36

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] = [         


        
相关标签:
1条回答
  • 2020-12-11 16:14

    The problem here is that you are not storing the result of from_base64 anywhere and then take a reference to it by calling as_slice. Chaining calls like that causes the result of from_base64 to go out of scope at the end of the line and the reference taken is no longer valid.

    extern crate rustc_serialize; // 0.3.24
    
    use rustc_serialize::base64::FromBase64;
    
    fn main() {
        let a: [u8; 30] = [0; 30];
        let b = a.from_base64().unwrap();
        println!("{:?}", b.as_slice());
    }
    
    0 讨论(0)
提交回复
热议问题