Working with the Rust book and failing to compile a simple integration test

You cannot access the functions of a binary crate (the one whose root file is src/main.rs) from outside it. As I wrote in your previous thread, the “integration test” feature of Cargo is intended to be used for two use cases:

  • Testing a library crate by using its public functions.
  • Testing a binary crate by running the program and seeing what it does.

Testing functions in the code of a binary crate is neither of those things — to write that kind of test, you must write the tests in the crate's own source code in src/, not in tests/. That's the only way the functions will be reachable.

Also, even if you could, the binary crate's name is not main, but hello_cargo (same as the library, confusingly); there is no crate or module named main. hello_cargo::... is the correct path to refer to the library (whose root file is src/lib.rs); main::... doesn't mean anything. (In general, lib.rs, main.rs, and mod.rs are names that are special and mean something other than the names of modules or crates.)

2 Likes