の評価を強制私がやろうとしています何の不自然な例です:フォース/ここで閉鎖署名
use std::boxed::Box;
#[derive(Debug)]
pub struct Foo<'a>(pub &'a str);
pub trait IntoBox {
fn into_box<'a>(self) -> Box<Fn(Foo) -> String>;
}
impl<B> IntoBox for B where B: Fn(Foo) -> String + 'static {
fn into_box(self) -> Box<Fn(Foo) -> String> { Box::new(self) }
}
fn direct_into_box<B: Fn(Foo) -> String + 'static>(b: B) -> Box<Fn(Foo) -> String> {
Box::new(b)
}
fn main() {
// Doesn't work
let x = IntoBox::into_box(|i| format!("{:?}", i));
// Works
let y = IntoBox::into_box(|i: Foo| format!("{:?}", i));
// Also works
let z = direct_into_box(|i| format!("{:?}", i));
}
私は私で行われているようにクロージャのと同じ評価を行うために、私の形質のimplを取得するにはどうすればよいですdirect_into_box
?私はdirect_into_box
と私のtrait implが同じように動作することを期待していました。
x
上のエラー:
error[E0271]: type mismatch resolving `for<'r> <[[email protected]<anon>:20:31: 20:53] as std::ops::FnOnce<(Foo<'r>,)>>::Output == std::string::String`
--> <anon>:20:13
|
20 | let x = IntoBox::into_box(|i| format!("{:?}", i));
| ^^^^^^^^^^^^^^^^^ expected bound lifetime parameter , found concrete lifetime
|
= note: concrete lifetime that was found is lifetime '_#29r
= note: required because of the requirements on the impl of `IntoBox` for `[[email protected]<anon>:20:31: 20:53]`
= note: required by `IntoBox::into_box`
error[E0281]: type mismatch: the type `[[email protected]<anon>:20:31: 20:53]` implements the trait `std::ops::Fn<(_,)>`, but the trait `for<'r> std::ops::Fn<(Foo<'r>,)>` is required (expected concrete lifetime, found bound lifetime parameter)
--> <anon>:20:13
|
20 | let x = IntoBox::into_box(|i| format!("{:?}", i));
| ^^^^^^^^^^^^^^^^^
|
= note: required because of the requirements on the impl of `IntoBox` for `[[email protected]<anon>:20:31: 20:53]`
= note: required by `IntoBox::into_box`
私は申し訳ありませんが、私はそれを遊び場で実行することはできません。どのようなアウトプットを得ましたか? – Jacob
私は以前も遊び場で問題を抱えていました。私は私の質問にエラー出力を追加しました。 –
あなたはdirect_into_boxでエラーを受け取らないでしょうか? grrr ...知らない。 – Jacob