Search code examples
rust

no method named `read_to_string` found for struct `File` in the current scope


I'm trying to read a file into a string, but i get a compiler error with the message "error[E0599]: no method named read_to_string found for struct File in the current scope"

Can't understand what I'm doing wrong. Here's the offending code below:

impl Todo {
    fn new() -> Result<Todo, std::io::Error> {
        let mut content = String::new();

        std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .open("todo.txt")?
            .read_to_string(&mut content)?
       
    }
}

Versions: cargo: 1.56.0 rustc: 1.56.1 rustup: 1.24.3


Solution

  • In order to use the trait method read_to_string, you have to bring Read trait into the scope.

    use std::io::Read;
    

    See also: