Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved Imports - "help: a similar path exists"

Tags:

rust

I'm fairly new to Rust and am trying to set up a new project. I'd like to have code for a parser, tokens, and addressing for a little 6502 project in separate files. My lib.rs file is therefore set up as:

pub mod parse;
pub mod tokens;
pub mod addressing;

I'm running into two problems. First, if I try to use one of these modules, for example parse, from main.rs with mod parse; I get this error:

unresolved module, can't find module file: parse.rs, or parse/mod.rs

Despite the fact that parse.rs is literally in the same folder as the main.rs file. (I also tried the 2015 method of moving module code into named folders each containing a mod.rs file, with the same result). I should also note that the structs and functions I want to use from each of these files are declared pub.

Secondly, if I try to use any of the functions or structs from other files inside any of these files, like use addressing; I get the error:

 --> src\parse.rs:2:5
  |
2 | use addressing;
  |     ^^^^^^^^^^ no external crate `addressing`

However, I am trying to use the Address struct here, and in the very same error message, the compiler suggests this!

error[E0412]: cannot find type `Address` in this scope
  --> src\parse.rs:10:14
   |
10 |     address: Address,
   |              ^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
2  | use crate::addressing::Address;
   |

So, I instead add use crate::addressing::Address; to the top of the file parse.rs, and a new error appears:

 --> src\parse.rs:2:12
  |
2 | use crate::addressing::Address;
  |            ^^^^^^^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `corten::addressing`

corten is the name of my project, so fair enough, let's try use corten::addressing;:

 --> src\parse.rs:2:5
  |
2 | use corten::addressing::*;
  |     ^^^^^^ use of undeclared crate or module `corten`
  |

...And, in fact, immediately underneath this error suggests the very same import syntax that it grumbled about earlier:

  --> src\parse.rs:10:14
   |
10 |     address: Address,
   |              ^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
2  | use crate::addressing::Address;
   |

What is going on here? The compiler has lead me in complete circles, each time complaining about its own suggestions and fixes.

I have also checked other answers on here and seem to have set up my files as suggested in their fixes (although some are on a slightly outdated edition of Rust by now). I have also checked how other projects on GitHub handle this and I don't seem to be doing anything different. Can I just use my struct?

like image 224
Rowan Avatar asked Aug 31 '25 20:08

Rowan


1 Answers

 --> src\parse.rs:2:12
  |
2 | use crate::addressing::Address;
  |            ^^^^^^^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `corten::addressing`
 --> src\parse.rs:2:5
  |
2 | use corten::addressing::*;
  |     ^^^^^^ use of undeclared crate or module `corten`
  |

These two errors put together are enough to pin down the mistake you have made: you have both a main.rs and a lib.rs, which is fine, and you have put mod parse; in both of them, which is not fine.

The key thing to know here to understand the problem is that main.rs and lib.rs are the roots of separate crates. Thus, they are different compilation contexts, and code that works in one will not work in the other. Not only that, if it did work, you'd then have two separately-compiled copies of the same items — and as far as the Rust compiler is concerned, you'd have two distinct Address types.

In general, only one mod foo; item should exist anywhere in your project for a given file foo.rs.

Anything that is needed in multiple crates should be in your library crate, i.e. some module which is (or has a parent that is) declared in lib.rs. Then you use corten::... to refer to it from main.rs or any other binary (tests/, examples/…).

It's fine to write whatever use declarations are useful in any given file, because those just bring things into scope (they shorten paths that you could have written in fully-qualified form). But do not ever duplicate a mod.

like image 187
Kevin Reid Avatar answered Sep 04 '25 10:09

Kevin Reid