我很难传递和评估关闭列表。大大简化,这个程序显示出与我正在尝试编写的程序相同的错误:
use std::vec::flat_map;
#[main]
fn main() {
let list:~[~fn()->~[~str]] = get_list();
//let res:~[~str] = flat_map(list, |&f|{f()});
let res:~[~str] = flat_map(list, apply);
println(res.to_str());
}
fn apply<T>(f:&fn()->T) -> T {
f()
}
fn get_list() -> ~[~fn()->~[~str]] {
~[
~||{~[~"foo"]},
~||{~[~"bar"]},
]
}
这基本上是试图获取返回列表的函数列表,并将其转换为运行函数的结果的平面列表。我得到两个编译器错误:
temp.rs:7:35: 7:40 error: mismatched types: expected `&fn<no-bounds>(&~fn:Send() -> ~[~str]) -> ~[<V3>]` but found `extern "Rust" fn(&fn<no-bounds>() -> <V4>) -> <V4>` (expected &-ptr but found fn)
temp.rs:7 let res:~[~str] = flat_map(list, apply);
^~~~~
temp.rs:16:2: 19:5 error: mismatched types: expected `~[~fn:Send() -> ~[~str]]` but found `~[~&fn<no-bounds>() -> ~[~str]]` (expected fn but found ~-ptr)
temp.rs:16 ~[
temp.rs:17 ~||{~[~"foo"]},
temp.rs:18 ~||{~[~"bar"]},
temp.rs:19 ]
error: aborting due to 2 previous errors
首先,apply
我注释掉的函数和 lambda 都不允许我映射函数列表。其次,在函数中get_list()
我无法生成可接受的向量。