I wrote the following code, I want to know why rustc keeps prompting me that I can’t find the method
As mentioned in the error message, you can only use methods from traits if the trait is in scope. The read_to_string
method is defined by the Read
trait, so you need to do use std::io::Read
to be able to call that method.
The reason for this is that I might want to define my own trait that has a read_to_string
method and implement it for File
- if trait methods were in scope by default, these methods would collide.
Alternatively, you can call std::fs::read_to_string
, which is a shortcut function for reading files to strings.
2 Likes
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.