0

I have the following code in main.rs

fn util_function() -> Result<String, std::io::Error> {
    Ok(String::from("OK"))
}

fn main() {
    let a = util_function().unwrap();
}

I split main.rs into two files:

// util.rs
fn util_function() -> Result<String, std::io::Error> {
    Ok(String::from("OK"))
}

and

// main.rs

mod util;

fn main() {
    let a = util::util_function().unwrap();
}

The problem that util.rs has a compilation error:

pub fn util_function(mut stream: &TcpStream) -> Result<String, std::io::Error> {
  |                                                            ^^^ Use of     undeclared type or module `std`

So why should I add using std; to the utils.rs while it is not necessary in main.rs?

Alex
  • 9,891
  • 11
  • 53
  • 87
  • Um....doesn't your question answer itself? I don't know Rust, but you're using `std::io::Error` in `utils.rs` and the error is telling you the compiler doesn't know what that is without the `using std;`. Presumably `using std;` would tell it what that is, vaguely like `include` in C and C++, `import` in Java, etc. So....? – T.J. Crowder Nov 26 '17 at 14:48
  • Not really, I don't need to do this in `main.rs` – Alex Nov 26 '17 at 14:49
  • Look at your (second) `main.rs`: Where do you see `std::io::Error`? (Or anything else from `std`?) Answer: You don't. :-) So the compiler doesn't need to know what it is when compiling `main.rs`. – T.J. Crowder Nov 26 '17 at 14:50

0 Answers0