E0432 no `read_csv` in the root

cargo +nightly build

outputs

error[E0432]: unresolved import `crate::read_csv`
  --> src/bin/gui.rs:31:5
   |
31 | use crate::read_csv;
   |     ^^^^^^^^^^^^^^^ no `read_csv` in the root

The project directory has

src/
├── bin
│   ├── ...
│   ├── gui.rs
│   └── ...
├── main.rs
├── ...
├── read_csv.rs
└── ...

src/main.rs has

mod read_csv;

How was the import unresolved?

main.rs and bin/gui.rs are two independent binary crates, they don't know about each other.

I didn't know that. Thanks. Well, the original code (and error) was

error[E0432]: unresolved import `jonathan`
  --> src/bin/gui.rs:31:5
   |
31 | use jonathan::read_csv;
   |     ^^^^^^^^ use of unresolved module or unlinked crate `jonathan`
   |
   = help: if you wanted to use a crate named `jonathan`, use `cargo add jonathan` to add it to your `Cargo.toml`

but Cargo.toml includes

[package]
name = "jonathan"
...

Well, is there a best practice for things like that? I just want to use read_csv.

You can’t import things from binary crates, only library crates. That use would work if:

  • you had a src/lib.rs file (the root file of your package's library crate), and
  • that file had read_csv defined in it.

Perhaps your src/main.rs was intended to be a src/lib.rs?

Well, I had the empty src/lib.rs.

If it is present (empty or not), you should not get the error "unresolved import jonathan" unless something else is wrong. What are the full contents of your Cargo.toml file?

Thanks kpreid. Well, I had deleted the src/lib.rs. I succeed the build now. Here is what I did.

I tried to add in src/lib.rs

mod read_csv;
mod types;

but I got

error[E0603]: module `read_csv` is private
  --> src/bin/gui.rs:31:15
   |
31 | use jonathan::read_csv;
   |               ^^^^^^^^ private module
   |
note: the module `read_csv` is defined here
  --> path/to/src/lib.rs:1:1
   |
 1 | mod read_csv;
   | ^^^^^^^^^^^^

I changed mod read_csv in src/lib.rs to

pub mod read_csv;

and build succeeded.

Well, is there a good reference for understanding the system of binary and library crates?

in this example, there are at least 3 different crates in the same package:

  • a binary crate gui, root module is source file src/bin/gui.rs
  • a binary crate jonathan (same as the package name), root module is src/main.rs
  • a library crate jonathan (same as the package name), root module is src/lib.rs

when you use crate::read_csv in each crate, the crate keyword refers to the root module of its own crate, so it will only work in main.rs where you have declared the module with mod read_csv;

when you write jonathan::read_csv, jonathan refers to the library crate. this can be resolved correctly if you have pub mod read_cvs; in lib.rs. it will NOT work with an empty lib.rs

see also my previous answer on these terms and concepts: Is a static in lib.rs unresolvable - #9 by nerditation

Thank you nerditation. I didn't expect there was also the gui binary crate. At first I was confused by the word "root module". I was thinking like "Isn't modules' root a directory?". Actually there are several roots!

Your link is also a great reading. I don't think I can understand everything you wrote right now, but I think I can learn from your writing while working on a project. Thank you.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.