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:

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

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

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

Now I get the following error:

error[E0583]: file not found for module `hasher`
 --> src/
1 | pub mod hasher;
  |         ^^^^^^
  = help: name the file either or hasher\ 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:

name = "tool"
version = "0.1.0"
authors = ["Jeramy<>"]
edition = "2018"

crypto-hash = "0.3.3"
filebuffer = "0.4.0"

name = "hasher"
path = "src/"

name = "tool"
path = "src/"
1 Like

What’s in your src/main. rs? And what happens if you name the file src/ 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

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

Moving the externs to 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.