【发布时间】:2017-03-16 12:43:48
【问题描述】:
我发现的所有有关刷新的文档都表明刷新标准输出的正确方法如下:
std::io::stdout().flush().expect("some error message");
这导致
no method named flush found for type std::io::Stdout in the current scope
我做错了什么?
【问题讨论】:
标签: rust
我发现的所有有关刷新的文档都表明刷新标准输出的正确方法如下:
std::io::stdout().flush().expect("some error message");
这导致
no method named flush found for type std::io::Stdout in the current scope
我做错了什么?
【问题讨论】:
标签: rust
您需要为Stdout 导入实现flush 方法的特征。
根据文档:
因此:
use std::io::Write; // <--- bring the trait into scope
fn main() {
std::io::stdout().flush().expect("some error message");
}
【讨论】:
谁能告诉我我做错了什么?
是的; 编译器已经这样做了。
fn main() {
std::io::stdout().flush().expect("some error message");
}
error[E0599]: no method named `flush` found for type `std::io::Stdout` in the current scope
--> src/main.rs:3:23
|
3 | std::io::stdout().flush().expect("some error message");
| ^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
candidate #1: `use std::io::Write;`
强调help 和note 行 - use std::io::Write。
大家一起:
use std::io::Write;
fn main() {
std::io::stdout().flush().expect("some error message");
}
【讨论】: