从Rust中的数组调用闭包 [英] Calling closures from an array in Rust

查看:164
本文介绍了从Rust中的数组调用闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何迭代闭包的数组,依次调用每个闭包?

How do I iterate over an array of closures, calling each one in turn?

对于函数,我发现我可以通过迭代数组来做到这一点,并取消引用产生的值:

With functions, I discovered I could do this by just iterating over the array, and dereferencing the values that produced:

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));
    }
}

然而,当我尝试对closures ,我得到一个错误:

However, when I try to do the same with closures, I get an error:

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 ,它仍然不起作用:

If I try declaring the iteration variable ref mut, it still doesn't work:

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

我在这里缺席吗?

推荐答案

.iter()方法的向量产生不可变的引用,你需要可变的调用闭包,因此你应该使用 .iter_mut()

The .iter() method of vector yields immutable references, you need mutable ones to call the closure, thus you should use .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

这篇关于从Rust中的数组调用闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆