如何迭代一系列闭包,依次调用每个闭包?
使用函数,我发现我可以通过迭代数组并取消引用生成的值来实现这一点:
fn square(x: int) -> int { x * x }
fn add_one(x: int) -> int { x + 1 }
fn main() {
let funcs = [square, add_one];
for func in funcs.iter() {
println!("{}", (*func)(5i));
}
}
但是,当我尝试对闭包执行相同操作时,我收到错误:
fn main() {
let closures = [|x: int| x * x, |x| x + 1];
for closure in closures.iter() {
println!("{}", (*closure)(10i));
}
}
产地:
<anon>:4:24: 4:34 error: closure invocation in a `&` reference
<anon>:4 println!("{}", (*closure)(10i));
^~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
<anon>:4:9: 4:41 note: expansion site
<anon>:4:24: 4:34 note: closures behind references must be called via `&mut`
<anon>:4 println!("{}", (*closure)(10i));
^~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
<anon>:4:9: 4:41 note: expansion site
如果我尝试声明迭代变量ref mut
,它仍然不起作用:
fn main() {
let closures = [|x: int| x * x, |x| x + 1];
for ref mut closure in closures.iter() {
println!("{}", (*closure)(10i));
}
}
结果:
<anon>:4:24: 4:39 error: expected function, found `&|int| -> int`
<anon>:4 println!("{}", (*closure)(10i));
^~~~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
<anon>:4:9: 4:41 note: expansion site
如果我添加另一个解除引用:
fn main() {
let closures = [|x: int| x * x, |x| x + 1];
for ref mut closure in closures.iter() {
println!("{}", (**closure)(10i));
}
}
我回到原来的错误:
<anon>:4:24: 4:35 error: closure invocation in a `&` reference
<anon>:4 println!("{}", (**closure)(10i));
^~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
<anon>:4:9: 4:42 note: expansion site
<anon>:4:24: 4:35 note: closures behind references must be called via `&mut`
<anon>:4 println!("{}", (**closure)(10i));
^~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
<anon>:4:9: 4:42 note: expansion site
我在这里缺少什么?是否有某些文档描述了它是如何工作的?
答案 0 :(得分:7)
vector的.iter()
方法产生不可变引用,你需要可变的引用来调用闭包,因此你应该使用.iter_mut()
:
fn main() {
let mut closures = [|x: int| x * x, |x| x + 1];
for closure in closures.iter_mut() {
println!("{}", (*closure)(10i));
}
}
-----
100
11