Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types .peek()的返回类型为';t可窥视<;T>;_Types_Rust - Fatal编程技术网

Types .peek()的返回类型为';t可窥视<;T>;

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

我正在尝试使用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()
}

有人能解释这个令人费解的结果吗?

您需要将函数指针从其特定的具体类型转换为非特定的函数指针类型:

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