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);
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::
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With