How can I return a reference to a local variable specifying that its lifetime is the same as self?
问题 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 ? 回答1: Explicit lifetime annotation of a reference cannot extend lifetime of an object it refers to. bytes is