`fn` 和 `||` 在函数数组类型上的差异 [英] Differences between `fn` and `||` in type for an array of functions

查看:49
本文介绍了`fn` 和 `||` 在函数数组类型上的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rust 中做了一些简单的实验,涉及到一组函数,最后得出了这个工作代码:

I was doing some simple experiments in Rust, involving an array of functions, and finally came out with this working code:

fn fun1(arg: &String) -> String {
    let prefix = String::from_str("a: ");
    prefix.add(arg)
}

fn fun2(arg: &String) -> String {
    let prefix = String::from_str("b: ");
    prefix.add(arg)
}

fn doall(arg: &String, funcs_vec: &[fn(&String) -> String]) {
    for f in funcs_vec.iter() {
        println!("{}", (*f)(arg));
    }
}

static funcs: &'static [fn(&String) -> String] = &[fun1, fun2];

fn main() {
    doall(&String::from_str("foo"), funcs);
}

打印:

a: foo
b: foo

然后,阅读一些关于SO的其他问题,我看到语法 |&String|->String 应该也可以工作,但是像这样尝试:

Then, reading some other question on SO, I saw the syntax |&String| -> String that should work as well, but trying it like this :

fn doall(arg: &String, funcs_vec: &[|&String| -> String]) {
    for f in funcs_vec.iter() {
        println!("{}", (*f)(arg));
    }
}

static funcs: &'static [|&String| -> String] = &[fun1, fun2];

编译器抱怨:

funcarray.rs:17:25: 17:45 error: missing lifetime specifier
funcarray.rs:17 static funcs: &'static [|& String| -> String] = &[fun1, fun2];
                                        ^~~~~~~~~~~~~~~~~~~~

如果我尝试将行更改为:

If I try to change the line to this:

static funcs: &'static [|&String|: 'static -> String] = &[fun1, fun2];

它给了我这个错误:

funcarray.rs:17:57: 17:70 error: mismatched types:
    expected `&'static ['static |&collections::string::String| -> collections::string::String:'static]`
    but found `&'static [fn(&collections::string::String) -> collections::string::String]`
(expected fn but found extern fn)
funcarray.rs:17 static funcs: &'static [|&String|: 'static -> String] = &[fun1, fun2];
                                                                        ^~~~~~~~~~~~~

在这里我很迷茫.我该怎么做才能让它发挥作用(如果可能的话)?

And here I'm pretty lost. What can I do to make it work (if it is possible) ?

更一般地说,这两种用于指定该数组中函数类型的语法有什么区别?好像牵扯到生生世世,但我想不通.

And more generally, what are the differences between these two syntaxes for specifying the type of the functions in this array ? It seems to involve lifetimes, but I can't figure it out.

推荐答案

Rust 中的函数指针和闭包完全不同.

A function pointer and a closure are quite different in Rust.

铁锈类型周期表列表

|T…| -> U
fn(T…) -> U

as Closure with mutable environment and Bare function type.

一个闭包捕获它的环境,一个函数没有.

A closure captures its environment, a function does not.

闭包不能在调用堆栈中向上传递,因为无法指定/验证捕获环境的生命周期,函数不捕获任何东西,因此可以返回em>.

A closure cannot be passed upwards in the call stack, because the lifetime of the captured environment cannot be specified/validated, a functions does not capture anything and can therefore be returned.

我们还没有(已经讨论过)有一个简短的语法来编写非捕获闭包,即.匿名函数.

We do not yet (there has been discussion) have a short syntax to write non-capturing closures, ie. anonymous functions.

如您所见,它们之间存在显着的语义和句法差异,并且它们不可互换.

As you see, there is a significant semantic and syntactic difference between these and they're not interchangeable.

阅读有关 Rust 中的闭包和函数的有趣博客文章是 Rust 中的 Fn 类型,取 3关闭和借用检查器.

Interesting blog articles to read about closures and functions in Rust are Fn Types in Rust, Take 3 and Closures and the Borrow Checker.

这篇关于`fn` 和 `||` 在函数数组类型上的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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