Calling closures in a Vec
Published:
Revised:
in
a029167
In rust one might want to have a list of closures, for example as a list of callbacks.
let mut fs: |> = Vec new;
fs.push;
for f in fs.iter f; // error: expected function but found `&||`
;
Maybe if we borrow f?
for &f in fs.iter f;
;
But then if we dereference f?
for f in fs.iter ; // error: closure invocation in a `&` reference
;
That’s not very helpful. Thanks to some friendly guys over at #rust
at irc.mozilla.org
I found something which works:
for f in fs.mut_iter ; // ok!
;
A note is that the closure types must be &mut ||
and not &||
.
In the end I think that the error messages could be more clear. But now we have a running example and all is well in the world!
// Print "imma firing my lazer"
let mut fs: |> = Vec new;
fs.push;
for f in fs.mut_iter ; // ok!
;