6
如何迭代一系列闭包,然后依次调用每个闭包?从Rust中的数组调用闭包
用的功能,我发现我可以只遍历数组过来,并取消引用所产生的价值做到这一点:
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
我在这里错过了什么?是否有文档介绍这是如何工作的?
啊,谢谢。有没有什么办法可以强制闭包是不可变的,所以它不需要通过可变引用来调用,作为解决问题的另一种方法? – 2014-10-10 14:14:01
嗯。使用'unboxed_closures'功能门尝试使用'[|&:x:int |'来完成此操作x * x,|&:x:int | x + 1]',但得到了不可思议的错误“错误:不匹配的类型:预计\'关闭\',找到\'关闭\'(预期关闭,找到关闭)”。无论如何,你回答了我的原始问题,我会看看我是否可以找出未装箱的关闭问题。 – 2014-10-10 14:28:55
@BrianCampbell我也在研究这个。看起来像取消装箱的关闭功能是有原因的。我认为类型推断仍然是越野车。 – Levans 2014-10-10 14:31:27