本文介绍了类型参数数量错误:应该为1,但发现为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试传递 std::io::BufReader 转换为功能:

I'm trying to pass a reference of std::io::BufReader to a function:

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

我在这里想念什么?

推荐答案

看看 BufReader 的实现清楚地表明BufReader具有必须指定的通用类型参数:

A look at the implementation of BufReader makes it clear that BufReader has a generic type parameter that must be specified:

impl<R: Read> BufReader<R> {

更改功能以说明type参数.您可以允许任何通用类型:

Change your function to account for the type parameter. You can allow any generic type:

use std::io::Read;

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

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

You could also use a specific concrete type:

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

这篇关于类型参数数量错误:应该为1,但发现为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 19:13