I am trying to run the following code. It was straightforward to understand that the lifetime of the returned value of inner()
was not compatible with the 'static
lifetime, but how can I make this work without changing the main()
function?
struct Wrapper<'a>(&'a str);
impl<'a> Wrapper<'a> {
fn inner(&self) -> &str {
self.0
}
}
pub fn main() {
let x = "hello";
let wrapper = Wrapper(&x);
let _: &'static str = wrapper.inner();
}
error[E0597]: `wrapper` does not live long enough
--> src/main.rs:12:27
|
12 | let _: &'static str = wrapper.inner();
| ^^^^^^^ borrowed value does not live long enough
13 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
I came across this as a example from a course I am following but I am kind of stuck with understanding how to make this work.