When I run use crate::feed; in src/cmdline.rs I expect that to import src/feed.rs, but it doesn't. Instead I get,
error[E0432]: unresolved import `crate::feed`
--> src/cmdline.rs:2:5
|
2 | use crate::feed;
| ^^^^^^^^^^^ no `feed` in the root
Despite the fact that src/feed.rs exists. However, if I panic and change it to mod feed; then I get
error[E0583]: file not found for module `feed`
--> src/cmdline.rs:2:1
|
2 | mod feed;
| ^^^^^^^^^
|
= help: to create the module `feed`, create file "src/cmdline/feed.rs"
Using mod super::
error: expected identifier, found keyword `super`
--> src/cmdline.rs:2:5
|
2 | mod super::feed;
| ^^^^^ expected identifier, found keyword
Or with use super::
error[E0432]: unresolved import `super::feed`
--> src/cmdline.rs:2:5
|
2 | use super::feed;
| ^^^^^^^^^^^ no `feed` in the root
File structure for files in question looks like this,
src/feed.rs
src/cmdline.rs
src/main.rs
I was able to make my imports work the way you describe by doing the following.
First in main.rs I import the module cmdline and each module I want to be able to use via crate::.
// file src/main.rs
mod cmdline;
mod feed; // <== import module to be able to use via `crate::feed`
fn main() {
cmdline::do_something();
}
Then in cmdline.rs I use create::feed.
// file src/cmdline.rs
use crate::feed; // <== import sibling module
pub fn do_something() {
feed::do_something_else();
}
And my feed.rs looks something like this.
// file src/feed.rs
pub fn d_something_else() {
// ...
}
From what I understand from experimenting is that you need to first use mod in your main.rs to define which modules are included in your crate.
I figured it out. The rust module system doesn't permit importing sibling files,
src/a.rssrc/b.rsFull stop: a.rs can not import b.rs. What it will do is try to source it from
src/a/b.rsIf you're on this answer, none of this probably makes sense to you and you've wasted hours on this. This was a source of my confusion:
src/main.rsIs actually special. Inside src/main.rs a mod will import a sibling file, (and also with the now deprecated mod.rs; or, with lib.rs). But the point is that your own rust files can't make use of rust code in sibling files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With