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

Steven Fackler sfackler at gmail.com
Sun Jun 1 17:27:44 PDT 2014


Yep!

main.rs:

mod foo;
mod bar;

fn main() {
    foo::foo();
}

foo.rs:

use bar; // this use lets you refer to the bar fn as bar::bar() instead of
::bar::bar()

pub fn foo() {
    bar::bar();
}

bar.rs:

pub fn bar() {}

Steven Fackler


On Sun, Jun 1, 2014 at 5:25 PM, Nicholas Bishop <nicholasbishop at gmail.com>
wrote:

> My intent wasn't to make bar a submodule of foo, but rather that foo &
> bar would be sibling modules (and foo just happens to use bar). Is
> there a way to do that?
>
> On Sun, Jun 1, 2014 at 6:56 PM, Steven Fackler <sfackler at gmail.com> wrote:
> > The directory layout of the project should match the module hierarchy.
> bar
> > is a submodule of foo so it shouldn't live next to foo in the filesystem.
> > There are a couple of filesystem setups that will work:
> >
> > src/
> >      main.rs
> >      foo/
> >          mod.rs
> >          bar.rs
> >
> > src/
> >      main.rs
> >      foo/
> >           mod.rs
> >           bar/
> >                mod.rs
> >
> > The first configuration seems to be what most code uses. If bar ends up
> > having submodules of its own, it would need to move to the second setup.
> >
> > Steven Fackler
> >
> >
> > On Sun, Jun 1, 2014 at 3:02 PM, Nicholas Bishop <
> nicholasbishop at gmail.com>
> > wrote:
> >>
> >> 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
> >> _______________________________________________
> >> Rust-dev mailing list
> >> Rust-dev at mozilla.org
> >> https://mail.mozilla.org/listinfo/rust-dev
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140601/2a51873f/attachment.html>


More information about the Rust-dev mailing list