Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

use std::io::Read;

Additional reference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading