11

I'm trying to read a single character from stdin, but I can't get it working. In different program, I used this exact same method and it worked.

let mut buffer = [0];
let _ = std::io::stdin().read(&mut buffer);
let a = buffer[0];

Compiling it gives this error:

src/befunge.rs:220:17: 220:31 error: failed to resolve. Use of undeclared type or module `std::io` [E0433]
src/befunge.rs:220      let _ = std::io::stdin().read(&mut buffer);
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
Fluffy
  • 731
  • 1
  • 8
  • 18
  • When asking questions on SO, please provide a [MCVE](http://stackoverflow.com/help/mcve) if possible, so that answers can be objectively checked to solve the issue. For your example, this could be [this simple program](https://play.rust-lang.org/?gist=43e3a4de6103ca986767bd304f54fe5e&version=stable&backtrace=0). – Matthieu M. Apr 07 '16 at 13:50

1 Answers1

20

I assume that befunge.rs is not your crate root, but a submodule. Paths like std::io::stdin() that are used outside of a use ...; declaration are relative to the current module, not absolute. To make the path absolute, prefix :: (like a prefixed / in unix paths) -> ::std::io::stdin(). Alternatively you can use some part of the path, like:

use std;
std::io::stdin();

or

use std::io;
io::stdin();

If you are using a subpath, like std::io more than once in your module, it's usually best to use it at the top.

If you are in the crate root, there is no difference between ::std and std, because the relative lookup path is the root. It only matters in submodules. Also: paths in use declarations are always absolute -- to make them relative to the current module prefix self::.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • I have a new problem with it that these solutions don't fix. I edited the main post. – Fluffy Apr 07 '16 at 13:34
  • Please ask only one question per SO-thread. So you should in theory create a separate question for your edit. But: this question has already been answered on SO, so better search for that. Spoiler: just read the whole error message of the compiler (including the note). – Lukas Kalbertodt Apr 07 '16 at 13:38
  • 1
    Do we have rationale in making "use" different, which is absolute, instead of relative to the current module? – WiSaGaN Apr 08 '16 at 05:21