错误的类型参数数量:预期为1但找到0

时间:2014-08-12 19:21:39

标签: generics rust

我试图将std::io::BufReader的引用传递给函数:

use std::{fs::File, io::BufReader};

struct CompressedMap;

fn parse_cmp(buf: &mut BufReader) -> CompressedMap {
    unimplemented!()
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buf = BufReader::new(File::open("data/nyc.cmp")?);

    let map = parse_cmp(&mut buf);

    Ok(())
}

我收到此错误消息:

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/main.rs:5:24
  |
5 | fn parse_cmp(buf: &mut BufReader) -> CompressedMap {
  |                        ^^^^^^^^^ expected 1 type argument

我在这里缺少什么?

1 个答案:

答案 0 :(得分:14)

查看implementation of BufReader表明BufReader具有必须指定的泛型类型参数:

impl<R: Read> BufReader<R> {

将您的功能更改为类型参数的帐户。您可以允许任何通用类型:

use std::io::Read;

fn parse_cmp<R: Read>(buf: &mut BufReader<R>)

您还可以使用特定的具体类型:

fn parse_cmp(buf: &mut BufReader<File>)