に拘束されているにもかかわらず、十分な長さ住んでいない:値は、次の錆のコードはコンパイルに失敗し
pub struct UserAction<'u> {
_act: &'u mut (FnMut() + 'u)
}
impl<'u, F: FnMut() + 'u> From<F> for UserAction<'u> {
fn from(f: F) -> Self {
UserAction { _act: (&mut f) as &'u mut (FnMut() + 'u) }
}
}
私はrustc
1.10 stableから取得するエラーは次のとおりです。
lives.rs:7:38: 7:39 error: `f` does not live long enough
lives.rs:7 UserAction { _act: (&mut f) as &'u mut (FnMut() + 'u) }
^
lives.rs:6:31: 8:10 note: reference must be valid for the lifetime 'u as defined on the block at 6:30...
lives.rs:6 fn from(f: F) -> Self {
^
lives.rs:6:31: 8:10 note: ...but borrowed value is only valid for the scope of function body at 6:30
lives.rs:6 fn from(f: F) -> Self {
^
error: aborting due to previous error
私はなぜこれがエラーであるのか分からない。タイプF
は少なくとも生涯の長さが'u
であり、それは制約されています。何が欠けていますか?このエラーを解決するにはどうすればよいですか?
ありがとう!私は 'Box'ベースのアプローチで終わりました。 –