Lifetime for primitive type

I found this example on rust book

struct Parser<'c, 's: 'c> {
    context: &'c Context<'s>,
}

I want something similar for byte array.

#[repr(C)]
struct vakiyam<'a, 'c: 'c> {
    data: &'a [u8]<'c>,
}

The Context struct internally contains some references to data, and those references are borrowed for lifetime 's.

A u8 or [u8] does not contain references to anything, so there shouldn't need to be a 'c lifetime to say how long the references it contains remain valid.

Thank you!!
Actually I want to do this

#[repr(C)]
struct vakiyam<'c> {
    data: &'c [u8],
}

fn get_vakiyam<'a, 'c: 'a>(data: &'a [u8]) -> &'c vakiyam {
    &vakiyam { data: data }
}

but I'm getting

  &vakiyam { data: data }
    |      ^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough

You're returning a reference to the temporary object. Try this instead:

fn get_vakiyam<'a>(data: &'a [u8]) -> vakiyam<'a> {
    vakiyam { data: data }
}
2 Likes

In this case, it seems you can actually elide the lifetimes from the function entirely:

#[repr(C)]
struct vakiyam<'c> {
    data: &'c [u8],
}

fn get_vakiyam(data: &[u8]) -> vakiyam {
    vakiyam { data: data }
}
2 Likes