Use std::io::prelude::* why do I need this to use the read_to_string() String?

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

fn main()
{
    let mut file = File::open("file.txt").expect("Can't open file");
    let mut contents = String::new();

    file.read_to_string(&mut contents).expect("Error");

    println!("\n\n{}", contents);
}

Shouldn't use std::fs::File; already have the read_to_string() function?

The read_to_string method is not defined on File, but on the trait Read, and to use a method from a trait, you must import the trait.

1 Like