[rust-dev] Error while trying to split source code into multiple files

Nicholas Bishop nicholasbishop at gmail.com
Sun Jun 1 15:02:54 PDT 2014


Here's example code:

/src/main.rs:
mod foo;
fn main() {
    foo::foo();
}

/src/bar.rs:
pub fn bar() {
}

/src/foo.rs:
mod bar;
pub fn foo() {
    bar::bar();
}

This fails:
$ rust-nightly-x86_64-unknown-linux-gnu/bin/rustc -v
rustc 0.11.0-pre-nightly (064dbb9 2014-06-01 00:56:42 -0700)
host: x86_64-unknown-linux-gnu

$ rust-nightly-x86_64-unknown-linux-gnu/bin/rustc main.rs
foo.rs:1:5: 1:8 error: cannot declare a new module at this location
foo.rs:1 mod bar;
             ^~~
foo.rs:1:5: 1:8 note: maybe move this module `foo` to its own
directory via `foo/mod.rs`
foo.rs:1 mod bar;
             ^~~
foo.rs:1:5: 1:8 note: ... or maybe `use` the module `bar` instead of
possibly redeclaring it
foo.rs:1 mod bar;
             ^~~
error: aborting due to previous error

I tried the first suggestion (moving foo.rs to foo/mod.rs), this fails too:
foo/mod.rs:1:5: 1:8 error: file not found for module `bar`
foo/mod.rs:1 mod bar;
                 ^~~

The second suggestion, which I took to mean replacing "mod bar" with
"use bar", also failed:
brokencrate/foo.rs:1:5: 1:8 error: unresolved import: there is no `bar` in `???`
brokencrate/foo.rs:1 use bar;
                         ^~~
brokencrate/foo.rs:1:5: 1:8 error: failed to resolve import `bar`
brokencrate/foo.rs:1 use bar;
                         ^~~
error: aborting due to 2 previous errors

I'm guessing that this failure is related to this RFC:
https://github.com/rust-lang/rfcs/blob/master/complete/0016-module-file-system-hierarchy.md

Unfortunately the RFC describes "a common newbie mistake" but not what
a newbie might do to correct this mistake. I also looked through
http://doc.rust-lang.org/tutorial.html#crates-and-the-module-system,
but didn't see this question directly addressed.

Thanks,
-Nicholas


More information about the Rust-dev mailing list