Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

generics - Wrong number of type arguments: expected 1 but found 0

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(())
}

I get this error message:

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

What am I missing here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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> {

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...