Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Proof that it is still quite confusing, I tried to remove the mod.rs in my project similarly to that example but now I don't understand how to access these modules.

With the mod.rs in place I can do this in my main.rs:

  pub mod foo
and then in a different file do:

  use crate::foo::bar::*
But without the mod.rs it doesn't work and I don't know what the correct incantation is...

Edit:

Nevermind, I thought this update meant you could remove the mod.rs completely but this is about declaring foo.rs as a file outside of the foo folder.

So you can't just do this like I was hoping:

  .
  ├── lib.rs
  └── foo/
    └── bar.rs


If you have

    foo/mod.rs
you can now use

    foo.rs
that's it. Contents of the file are exactly the same.

Nothing to do with changing the mod line, or the use line. Those all stay the same. It's about files in the filesystem.


It's my bad I thought I could remove the mod.rs that just redeclares the submodules in my case.


If you'd like to have two modules, `main.rs` and `foo/bar.rs`, and you want the latter to be `foo::bar`, you can do this in `main.rs` to avoid needing a foo.rs that just does `mod bar;`:

    mod foo {
        mod bar;
    }


I was just following the example from the docs, my use case is more like this:

  .
  ├── lib.rs
  └── foo/
    └── mod.rs
    └── one.rs
    └── two.rs
    └── three.rs
    └── four.rs
  └── bar/
    └── mod.rs
    └── one.rs
    └── two.rs
    └── three.rs
    └── four.rs
Which feels to me like a very common folder structure in other languages.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: