John Kugelman
John Kugelman

Reputation: 361595

Can't write `use std::{fs::self, io::self}` due to multiple uses of `self`

Code

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

fn rmdir(path: impl AsRef<std::path::Path>) -> io::Result<()> {
    fs::remove_dir(path)
}

Error

error[E0430]: `self` import can only appear once in an import list
 --> src/lib.rs:2:5
  |
2 |     fs::self,
  |     ^^^^^^^^ can only appear once in an import list
3 |     io::self,
  |     -------- another `self` import appears here

(Playground)

Why can't I write module::self with two different modules? I thought I might use modules with ::self to make it clear they're modules, not functions.

If I add curly brace it's allowed:

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

Is there a good reason for this, or is it a compiler bug/language design flaw?

Upvotes: 1

Views: 211

Answers (1)

Asti
Asti

Reputation: 12667

Generally, you'd expect to use use std::io::{self, BufReader} to mean import std::io and std::io::BufReader.

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

is just

use std::{ fs, io };

E0430 is a check for two selfs in the same {} scope. This makes something like

use something::{self, self}; 

illegal. When you put each ::self within its own scope, it's legal.

Upvotes: 2

Related Questions