Rust reading a file

Viewed 2343

I'm trying to read a file named enable.txt that's in the same dir as my main.rs, and my code looks like this:

use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::error::Error;
fn main() {
    let path = Path::new("enable.txt");
    let display = path.display();
    let mut file = File::open(&path);
    let mut contents = String::new();
    file.read_to_string(&mut contents);
    println!("{}", contents);
}

When I compile it with either cargo run or rustc src/main.rs, I get this error message:

error: no method named `read_to_string` found for type `std::result::Result<std::fs::File, std::io::Error>` in the current scope
  --> src/main.rs:10:10
   |
10 |     file.read_to_string(&mut contents);
   |          ^^^^^^^^^^^^^^
1 Answers
Related