After I was comfortable enough in Rust to start writing reasonable projects, I soon encountered modules as a necessary subject, and it confused me so much. I decided to take a couple hours to read docs, play around with some drop-dead simple code that would organize code into modules as either `mod foo { }` blocks, single file modules (foo.rs) or modules with their own submodules, organized by directory (foo/mod.rs with foo/bar.rs).<p>It pretty much instantly became obvious how to use them, and in retrospect, I don't know why it was so confusing. I think it's a good example of how important it is to [a] apply deliberate practice/learning and [b] have empathy for others that haven't (yet) progressed to our own level of understanding.
Modules confuse me greatly. I was trying to have both a lib folder and a main.rs and I couldn’t figure it out in 15-30 minutes, so I gave up and just put the module in src as a sibling to main.rs while I got stuff done - come to think of it, I’d like to revisit that.
IMHO you should keep your src folder flat and use multiple crates in a workspace for hierarchy instead of nested modules. People forget that the compilation unit of Rust is a crate, not a source file.
I think the most surprising thing about Rust modules is that you need to """pre-declare""" them, especially since Rust doesn't really use function prototypes.<p>So when you try to move a module into its own file, you need to keep the `mod foo` in the original file.
importing a module is the most fustrating part of writing rust code next to writing a multiple-spawn tasks.<p>Can someone help me how to import a module in lib/m.rs from lib.rs ? I've never seen someone asked about it.