質問

閉鎖を試しています:

fn call_it(f: ||) {
    f(); 
}
let klosure = || println("closure!");
call_it(klosure);
call_it(klosure); //Blows up here
.

call_it()へのX個への渡し2回の閉鎖値のアカウントでのコンパイラエラーが発生します。

closures.rs:16:13: 16:20 error: use of moved value: `klosure`
closures.rs:16     call_it(klosure);
                           ^~~~~~~
closures.rs:15:13: 15:20 note: `closure` moved here because it has type `||`, which is a non-copyable stack closure (capture it in a new closure, e.g. `|x| f(x)`, to override)
closures.rs:15     call_it(klosure);
                           ^~~~~~~
.

コンパイラは実際に問題を解決する方法についての提案をしますが、私はそれを正常に適用する方法を考え出していません。

提案?:d

役に立ちましたか?

解決

注: `closure`はここで移動しました。/ P>

これは、|| closure()の代わりにclosureを書き込むことを意味します。

他のヒント

(これをしないでください。これをしないでください、それは近い将来認知されないかもしれません。&mut ||はおそらく今罰金と将来的に働きます。この答えのコメントの議論とリンクを参照してください。)

もう1つの潜在的なアプローチ(読み取るビットuglierです):

fn call_it(f_ref: &||) { // now takes a borrowed reference to a closure
    (*f_ref)();
}
let klosure = || println("closure!");
call_it(&klosure);
call_it(&klosure);
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top