I'd like to write some code like below
struct SomeData(u8, u8);
impl SomeData {
fn to_bytes(&self) -> &[u8] {
let mut bytes: [u8; 16] = [0; 16];
// fill up buffer with some data in `SomeData`.
bytes[0] = self.0;
bytes[1] = self.1;
// return slice
&bytes[..]
}
}
I know the reason why above code doesn't work. How can I return a reference specifying that its lifetime is the same as self?
bufferis a local variable here ? What is the link withself? Why don't just move the value ? - Stargateur