Problems finding module when compiling

I’m trying to create a project that builds both a lib and a bin, but I’m having problems getting the compiler to recognize a module when building.

Here’s my project tree:
image

Here’s the error I get when I have the module in the src dir:

error[E0583]: file not found for module `hasher`
 --> src\lib.rs:1:9
  |
1 | pub mod hasher;
  |         ^^^^^^
  |
  = help: name the file either lib\hasher.rs or lib\hasher\mod.rs inside the directory "src"

So I’ve done what the compiler suggests and move the file to lib\hasher like so:
image

Now I get the following error:

error[E0583]: file not found for module `hasher`
 --> src/lib.rs:1:9
  |
1 | pub mod hasher;
  |         ^^^^^^
  |
  = help: name the file either hasher.rs or hasher\mod.rs inside the directory "src"

I have no idea how to get this to compile. Any advice?

Here’s the contents of my Cargo.toml if that matters:

[package]
name = "tool"
version = "0.1.0"
authors = ["Jeramy<none@none.com>"]
edition = "2018"

[dependencies]
crypto-hash = "0.3.3"
filebuffer = "0.4.0"

[lib]
name = "hasher"
path = "src/lib.rs"

[[bin]]
name = "tool"
path = "src/main.rs"
1 Like

What’s in your src/main. rs? And what happens if you name the file src/hasher.rs and remove these sections from your Cargo.toml?

So I figured it out, but I don’t understand part of the problem.

I placed my extern crates in the lib mod and that helped, but I also had to remove pub mod lib; from main.rs.

@carols10cents, I had the externs in main.rs and pub mod lib; I did what you suggested, removing the stuff from the config and naming the file src/hasher.rs and it still failed the same way.

Moving the externs to lib.rs and removing pub mod lib fixed the issue, although I don’t understand why. I also don’t understand why I was getting the error messages and why they would change to back and forth when I followed the compilers instructions.

Are you trying to use modules directly in the bin?

The usual approach is:

  • Only the library part uses modules. All modules belong there. Some may be public.

  • The bin includes the library

  • The bin is one file only. It only uses modules indirectly via the library.

  • Module files are never in both bin and lib.

If that is to limiting, split the project into two crates.

I think having pub mod lib; in bin was the biggest problem.

lib is a starting point for a library. You can’t reuse it as a module. The whole library will be compiled and made available to you in the binary automatically.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.