【发布时间】:2018-01-19 15:32:42
【问题描述】:
我正在尝试使用 Rust 1.22.1 版本编译以下代码:
use std::str::Split;
use std::iter::Peekable;
// This is fine...
fn tokenize<'a>(code: &'a str) -> Split<'a, fn(char) -> bool> {
code.split(char::is_whitespace)
}
// ...but this is not...
fn tokenize_peekable_bad<'a>(code: &'a str) -> Peekable<Split<'a, fn(char) -> bool>> {
code.split(char::is_whitespace).peekable()
}
// ...however this is?
fn tokenize_peekable<'a>(code: &'a str) -> Peekable<Split<'a, fn(char) -> bool>> {
tokenize(&code).peekable()
}
在我看来tokenize_peekable_bad 和tokenize_peekable 应该具有完全相同的类型签名,但是tokenize_peekable_bad 给出了编译器错误,而tokenize_peekable 很好。
错误是
error[E0308]: mismatched types
--> src/main.rs:11:5
|
11 | code.split(char::is_whitespace).peekable()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found fn item
|
= note: expected type `std::iter::Peekable<std::str::Split<'a, fn(char) -> bool>>`
found type `std::iter::Peekable<std::str::Split<'_, fn(char) -> bool {std::char::<impl char>::is_whitespace}>>`
谁能解释一下这个令人费解的结果?
【问题讨论】: