Why is there no `use std :: io`?

I tried to compile the following program:

use std::io;

fn main() {
    io::stdout().write(b"Please enter your name: ");
    io::stdout().flush();
}

Unfortunately, the compiler resisted:

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`

I found what I need to do use std::io::{self, Write};. What actually does use std::io;and how (if possible) I pull out all the names defined in std::io? Also, would that be a bad style?

+4
source share
1 answer

What does use std::io;it really do?

It does what each use statement does: makes the last part of the path used directly available (by pulling it into the current namespace). This means that you can write io, and the compiler knows what you mean std::io.

How can I pull out all the names defined in std::io?

use std::io::*;. .

, ?

, . . , . , std::fmt::Write... fmt io . , -.

, glob-import: preludes. std::io::prelude, . . .

+11

Source: https://habr.com/ru/post/1664045/


All Articles