Types .peek()的返回类型为';t可窥视<;T>;
我正在尝试使用Rust版本1.22.1编译以下代码:Types .peek()的返回类型为';t可窥视<;T>;,types,rust,Types,Rust,我正在尝试使用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 toke
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()
}
有人能解释这个令人费解的结果吗?您需要将函数指针从其特定的具体类型转换为非特定的函数指针类型:
fn tokenize_peekable_ok_now<'a>(code: &'a str) -> Peekable<Split<'a, fn(char) -> bool>> {
code.split(char::is_whitespace as fn(char) -> bool).peekable()
}
fn标记化\u可查看\u确定\u现在可查看bool{specific}>
->Split