【问题标题】:Wrong number of type arguments: expected 1 but found 0类型参数的数量错误:预期为 1,但发现为 0
【发布时间】:2014-08-12 19:21:39
【问题描述】:

我正在尝试将 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

我在这里错过了什么?

【问题讨论】:

    标签: generics rust


    【解决方案1】:

    查看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>)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      相关资源
      最近更新 更多