我正在尝试将文件读入字符串,但出现编译器错误,并显示消息“错误[E0599]:在当前范围内没有read_to_string
为结构找到名为的方法”File
无法理解我做错了什么。下面是有问题的代码:
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)?
}
}
Run Code Online (Sandbox Code Playgroud)
版本: 货物:1.56.0 rustc:1.56.1 rustup:1.24.3
为了使用Trait 方法read_to_string
,您必须将Read
Trait引入作用域。
use std::io::Read;
Run Code Online (Sandbox Code Playgroud)
也可以看看: