我试图编译下面的程序:为什么在这里不使用std :: io?
use std::io;
fn main() {
io::stdout().write(b"Please enter your name: ");
io::stdout().flush();
}
不幸的是,编译器拒绝:
error: no method named `write` found for type `std::io::Stdout` in the current scope
--> hello.rs:4:18
|
4 | io::stdout().write(b"Please enter your name: ");
| ^^^^^
|
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
= help: candidate #1: `use std::io::Write`
我发现,我需要做的use std::io::{self, Write};
。 use std::io;
实际上是做什么,以及如何做(如果可能)我拉std::io
定义的所有名称?另外,它会是一个不好的风格?
为什么我需要写入但不刷新?另外,不是那些标准输出/标准输入的方法? – d33tah
'write()'和'flush()'是[Write'特征](https://doc.rust-lang.org/std/io/trait.Write.html)的方法。因此你只需要使用那个特质。 'Stdout'是一种实现* Write特性的类型。方法签名由特性给出,由类型本身实现。这是否澄清了这个问题? :) –
所以'使用std :: io;'拉类型,但不是特征? – d33tah