【发布时间】:2014-09-17 10:10:29
【问题描述】:
我想创建一个函数向量
let all_rerankers = vec![ match_full
, match_partial
, match_regex
, match_camel_case
];
但是,match_camel_case 比其他函数需要多一个参数,所以我虽然可以为 match_camel_case 定义一个闭包
// 3 is the extra parameter needed by match_camel_case
let close_camel_case = |str: &str, keyword: &str| {
match_camel_case(str, keyword, 3)
};
然后指定我的向量的类型:
let all_rerankers: Vec<|str: &str, kwd: &str| -> MatchScore>
= vec![ match_full
, match_partial
, match_regex
, close_camel_case
];
但是编译它告诉我 Rust 对待它们的方式不同:
mismatched types: expected `fn(&str, &str) -> MatchScore`,
found `|&str, &str| -> MatchScore`
(expected extern fn, found fn)
close_camel_case
^~~~~~~~~~~~~~~~
(以及我的vec! 宏中的类似类型错误)
它似乎也区分了Fn 类型和闭包类型。我可以通过将每个 match_* 函数包装在一个闭包中来进行编译,但我确信有更好的解决方案。
问题:
- 这里的实际不匹配是什么?错误消息似乎暗示
Fnvs 闭包类型,但错误消息中还有expected extern fn, found fn - 如何使类型匹配? (即把闭包转换成
fn类型,因为它是纯的)
我的 rustc 版本:rustc 0.12.0-pre-nightly (09cebc25a 2014-09-07 00:31:28 +0000)(如果需要可以升级)
【问题讨论】: