GraphicalDot
GraphicalDot

Reputation: 2821

Rust modules, combine two use statements

From documentation

use std::io;
use std::io::Write;

Two use statements where one is a subpath of the other

The common part of these two paths is std::io, and that’s the complete first path. To merge these two paths into one use statement, we can use self in the nested path, as shown in Listing

use std::io::{self, Write};

Combining the paths into one use statement

This line brings std::io and std::io::Write into scope.

if i brought std::io in the scope, isn't it obvious that io::Write will be available automatically in the scope provided Write is a public item? Why it needs to be imported separately?

Upvotes: 1

Views: 492

Answers (1)

imbrn
imbrn

Reputation: 151

If I understood well your question, I think you're misunderstanding the use statements.

It seems to me that you're thinking the statement use std::io is going to bring all public stuff from inside it. But that's not the truth. It's just going to create an alias for std::io as just io, so you can use it as in io::Result or io::Write. If you want to bring all public stuff from that, you should use std::io::* instead, but that's not recommended, as it's going to pollute your namespace.

Upvotes: 1

Related Questions