3 ms·
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 de
by mynameisash 4y ago
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).
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.
- bobbylarrybobby 4y agoNote that the "new" way to do modules with your file hierarchy is to have foo.rs instead of foo/mod.rs. foo.rs is at the same level as folder foo, foo/mod.rs is gone, and everything else is the same. Not sure whether this is truly better, but it does make it much easier to search for the file containing the module foo -- with the old way, you would have one mod.rs per module which quickly got painful when searching.
- pjmlp 4y agoOnly for simple modules, if they get complex enough, one will quickly use the "old" approach with multiple files.
- naavis 4y agoI had exactly the same experience. It's a simple system, but felt very confusing at first.