Return an slice from a previously created array

Hi, I have this code :
let key = match args_vec.get(0) {

        Some(&"rand") => &rand::random::<[u8; 16]>(),

        Some(k) => k.as_bytes(),

        None => {

            msg.reply(ctx, "input a key").await?;

            return Ok(())



The rust compiler complains because rand::random::<[u8; 16]>() does not live long enough. How can I extend his lifetime so I can successfully return a slice from it?

You can't return references to something that you create on the spot. You could return the array by value, but since you need the types to match with the non-random case, why don't you just turn both values (random() and k) into a Vec instead?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.