Rust - Why to import a trait when it's already implemented on struct

Viewed 64

In the below example from std docs, we're importing the read trait from std::io::prelude::* although the trait is implemented on File.

Why can't we implicitly use the f.read() method on File instance? Does importing the trait solve any specific problem?

use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut f = File::open("foo.txt")?;
    let mut buffer = [0; 10];

    // read up to 10 bytes
    let n = f.read(&mut buffer)?;

    println!("The bytes: {:?}", &buffer[..n]);
    Ok(())
}
0 Answers
Related