Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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

like image 861
scroobius Avatar asked Sep 06 '25 03:09

scroobius


1 Answers

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:

  • Why do I need to import a trait to use the methods it defines for a type?
like image 86
Abdul Niyas P M Avatar answered Sep 07 '25 21:09

Abdul Niyas P M