私は、タイプがFn() ->()
のクロージャのコレクションを取る関数を書こうとしています。すなわち、それぞれのクロージャはargsを取りません。何も返しません(実際にはすべての環境をクロージャオブジェクトに移動するためにFnOnce
にします) 。クロージャのコレクションを取得する関数を作成するにはどうすればよいですか?
私は多くのことを試しましたが(Box<Fn() ->()>
を使用し、&'static
を使用しています)、私はこの動作を得ることができません。
Rust Playgroundに、私がしようとしていることを示すために要点を作成しました。
ここでは単純化されたコードです:
fn run_all_tests<I>(tests: I)
where
I: IntoIterator<Item = Box<FnOnce() ->()>>,
{
}
fn main() {
let examples = [1, 2, 3];
run_all_tests(examples.iter().map(
|ex| Box::new(move |ex| assert!(ex > 0)),
));
}
エラー:
error[E0271]: type mismatch resolving `<[[email protected]/main.rs:11:9: 11:49] as std::ops::FnOnce<(&{integer},)>>::Output == std::boxed::Box<std::ops::FnOnce() + 'static>`
--> src/main.rs:10:5
|
10 | run_all_tests(examples.iter().map(
| ^^^^^^^^^^^^^ expected closure, found trait std::ops::FnOnce
|
= note: expected type `std::boxed::Box<[[email protected]/main.rs:11:23: 11:48]>`
found type `std::boxed::Box<std::ops::FnOnce() + 'static>`
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Map<std::slice::Iter<'_, {integer}>, [[email protected]/main.rs:11:9: 11:49]>`
= note: required by `run_all_tests`