Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the `std` module undeclared? [duplicate]

Tags:

rust

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);
like image 618
Fluffy Avatar asked Apr 07 '16 12:04

Fluffy


1 Answers

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::.

like image 153
Lukas Kalbertodt Avatar answered Oct 19 '22 09:10

Lukas Kalbertodt