Unresolved import error

Cargo check complains about unresolved imports

āÆ cargo check
    Checking nzb-parser v0.1.0 (redacted)
error[E0432]: unresolved imports `nzb_parser::File`, `nzb_parser::Meta`, `nzb_parser::Segment`
 --> src\parser.rs:2:18
  |
2 | use nzb_parser::{File, Meta, Segment};
  |                  ^^^^  ^^^^  ^^^^^^^ no `Segment` in the root
  |                  |     |
  |                  |     no `Meta` in the root
  |                  no `File` in the root
  |
  = help: consider importing this struct instead:
          std::fs::File

error[E0432]: unresolved import `nzb_parser::NZB`
 --> src/main.rs:4:5
  |
4 | use nzb_parser::NZB;
  |     ^^^^^^^^^^^^^^^ no `NZB` in the root

For more information about this error, try `rustc --explain E0432`.
error: could not compile `nzb-parser` (bin "nzb-parser") due to 2 previous errors

Cargo run however successfully executes and prints the "unresolved" struct:

āÆ cargo run  
   Compiling nzb-parser v0.1.0 (redacted)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.01s
     Running `target\debug\nzb-parser.exe`
NZB { meta: truncated, files: truncated }

Here's my structure and as you can see lib.rs defines the structs in question.

āÆ lsd --tree --icon never .\src\
src
ā”œā”€ā”€ lib.rs
ā”œā”€ā”€ main.rs
ā”œā”€ā”€ parser.rs
ā””ā”€ā”€ utils.rs

āÆ rg "pub struct (Meta|Segment|NZB|File)"
src\lib.rs
6:pub struct Meta {
31:pub struct Segment {
48:pub struct File {
75:pub struct NZB {

It's looking for a dependency or submodule called nzb_parser, and you're not a dependency of yourself.

Use crate instead of the name of the current crate.

use crate::{File, Meta, Segment};

Diagnostics could be better here.

1 Like

Using crate:: results in a compile error:

error[E0432]: unresolved imports `crate::File`, `crate::Meta`, `crate::Segment`
 --> src\parser.rs:2:13
  |
2 | use crate::{File, Meta, Segment};
  |             ^^^^  ^^^^  ^^^^^^^ no `Segment` in the root
  |             |     |
  |             |     no `Meta` in the root
  |             no `File` in the root
  |
  = help: consider importing one of these structs instead:
          std::fs::File
          nzb_parser::File
  = help: consider importing this struct instead:
          nzb_parser::Meta
  = help: consider importing this struct instead:
          nzb_parser::Segment

error[E0432]: unresolved import `crate::NZB`
 --> src/main.rs:4:5
  |
4 | use crate::NZB;
  |     ^^^^^^^^^^ no `NZB` in the root
  |
help: consider importing this struct instead
  |
4 | use nzb_parser::NZB;
  |     ~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0432`.
error: could not compile `nzb-parser` (bin "nzb-parser") due to 2 previous errors

Do you have a mod parser; in your main.rs maybe? If you have a library crate and binary crates within the same package, you should only define modules in your library (lib.rs) and not in your binaries (like main.rs). Inside of your library and its submodules you can import types from other modules in the library with use crate:: and in your main.rs you can import public items from your library with use nzb_parser::.

3 Likes

That was it. I had mod parser; and mod utils; in both lib.rs and main.rs. But how do I access private functions in main.rs?

error[E0432]: unresolved import `parser`
 --> src/main.rs:2:5
  |
2 | use parser::{parse_files, parse_metadata};
  |     ^^^^^^ use of undeclared crate or module `parser`

error[E0432]: unresolved import `utils`
 --> src/main.rs:4:5
  |
4 | use utils::read_xml_file;
  |     ^^^^^ use of undeclared crate or module `utils`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `nzb-parser` (bin "nzb-parser") due to 2 previous errors

parse_files, parse_metadata, and read_xml_file are all using pub(crate) fn ...

Your binary is not the same crate as your library, they are distinct crates (within the same package). Therefore your binary can only access the public interface of your library. I'd just make all items your binary needs access to public.

1 Like

Okay, I'll make them public. Thank you