You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
traitGoal{}implGoalfor(){}structHolder<'a,T: ?Sized>{r:&'a T,}structStaticHolder<T>{t:T,}fnmake_goal<'a,T:'a>(_s:&'a StaticHolder<T>) -> implGoal + 'static {()}fnouter() -> implGoal{let s = String::from("hello world");let h = Holder{r: s.as_str()};let s = StaticHolder{t: h };make_goal(&s)}fnmain(){outer();}
I expected it to work. The value returned from make_goal is impl Goal + 'static, so i should be able to return it from outer.
Instead, this happened:
error[[E0597]](https://doc.rust-lang.org/stable/error_codes/E0597.html): `s` does not live long enough
--> src/main.rs:19:25
|
18 | let s = String::from("hello world");
| - binding `s` declared here
19 | let h = Holder { r: s.as_str() };
| ^^^^^^^^^^ borrowed value does not live long enough
20 | let s = StaticHolder { t: h };
21 | make_goal(&s)
| ------------- argument requires that `s` is borrowed for `'static`
22 | }
| - `s` dropped here while still borrowed
For more information about this error, try `rustc --explain E0597`.
Meta
I compiled in the playground using 1.72.0 and 1.74.0.
The text was updated successfully, but these errors were encountered:
structRef<'a>(&'a ());fncapture<'o,T>(_x:T) -> implSend + 'o {}fnoutlives<'o,T:'o>(_t:T){}fntest<'o>(x:()) -> implSend + 'o {outlives::<'o,_>(capture::<'o,Ref<'_>>(Ref(&x)));// OK.capture::<'o,Ref<'_>>(Ref(&x))// ^^// ERROR: borrowed value does not live long enough.}
As it applies here, it's not that the argument lifetimes need to be 'static, it's that they have to be able to be named in a way that's valid outside of the function. Consider, e.g., that this is accepted:
traitCaptures<T>{}impl<T,U: ?Sized>Captures<T>forU{}structRef<'a>(&'a ());fncapture<'o,T>(_x:T) -> implSend + 'o {}fnoutlives<'o,T:'o>(_t:T){}fntest<'o,'b>(x:&'b ()) -> implSend + Captures<&'b ()> + 'o {outlives::<'o,_>(capture::<'o,Ref<'b>>(Ref(x)));// OK.capture::<'o,Ref<'b>>(Ref(x))// OK.}
I tried this code (playground):
I expected it to work. The value returned from
make_goal
isimpl Goal + 'static
, so i should be able to return it fromouter
.Instead, this happened:
Meta
I compiled in the playground using 1.72.0 and 1.74.0.
The text was updated successfully, but these errors were encountered: