Types 从Rust中的数组调用闭包
如何迭代闭包数组,依次调用每个闭包 对于函数,我发现我可以通过迭代数组并取消引用生成的值来实现这一点:Types 从Rust中的数组调用闭包,types,closures,rust,Types,Closures,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 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
结果:
<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
我回到最初的错误:
<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
:4:24:4:35错误:`&`引用中的闭包调用
:4 println!({},(**结束)(10i));
^~~~~~~~~~~
注意:在格式参数的扩展中!
:2:23:2:77注:扩展站点
:1:1:3:2注:在println的展开中!
:4:9:4:42注:扩展站点
:4:24:4:35注意:引用后面的闭包必须通过`&mut调用`
:4 println!({},(**结束)(10i));
^~~~~~~~~~~
注意:在格式参数的扩展中!
:2:23:2:77注:扩展站点
:1:1:3:2注:在println的展开中!
:4:9:4:42注:扩展站点
我错过了什么?是否有文档描述了这是如何工作的?向量的.iter()
方法产生不可变的引用,您需要可变的引用来调用闭包,因此您应该使用.iter\u mut()
:
啊,谢谢。有没有办法强制一个闭包是不可变的,这样就不需要通过可变引用来调用它,作为解决问题的另一种方法?嗯。尝试使用
unboxed|u闭包
featuregate来实现这一点,使用[|和:x:int|x*x,|和:x:int|x+1]
,但得到了无法改变的错误错误:不匹配的类型:应为`closure`,找到`closure`(应为closure,找到closure)“。不管怎样,你回答了我最初的问题,我会看看我是否能解决未装箱的结案问题。@BrianCampbell我也在研究这个问题。”。看起来,取消装箱的闭包被功能选通是有原因的。我认为类型推断仍然存在缺陷。Rust 0.11的等效语法是什么?它没有iter_mut()。@mcandre它可能是。mut_iter()
。然而,我们并不建议继续使用旧版本的Rust,因为该语言仍在快速变化。
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
fn main() {
let mut closures = [|x: int| x * x, |x| x + 1];
for closure in closures.iter_mut() {
println!("{}", (*closure)(10i));
}
}
-----
100
11