6 ms·
Learning Rust – Day 4 – Understanding Modules
- brianhorakh 4y agoDer Weg ist das Ziel!
- mrlonglong 4y agoThe way is the goal!
- faitswulff 4y agoModules 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.
- nkozyra 4y agoI haven't seen this as being discouraged. Rust does more things differently than most newer languages so the learning curve is there but the documentation is fantastic if you have the time or patience. I say this as someone who has stumbled my way through to competence/expertise on some good newer languages (zig, nim, go without RTFM very much, but I can't do that with Rust. But there's also a relatively early moment when you get to a local maxima and suddenly it all starts coming together.
- lumost 4y agohonest question, what necessitated Rust's choice of using such a different module system? Every other modern language seems to roughly map directory to module (go/python etc), or explicitly declare a folder as a module (java). C/C++ is the outlier, effectively using file as a module. Why is Rust so different on this seemingly boring topic?
- AlchemistCamp 4y agoRust's newer scheme is basically the same way it works by convention in Elixir (a more modern language than Golang or the others you listed). The older mod.rs scheme seems like an unneeded special case to me, but even that isn't exactly alien compared to what other popular programming languages have done before.
- dragonwriter 4y ago> Every other modern language seems to roughly map directory to module (go/python etc) [...]. C/C++ is the outlier, effectively using file as a module. Actually, no, Python uses files as modules.
- pjmlp 4y agoOr a directory containing an __init__.py file inside,. describing which modules to import when the directory is imported.
- dragonwriter 4y agoThat's a package, not a module.
- pjmlp 4y agoIn Python speak yes, in CS speak, it depends on which language we are talking about. I guess if you want to be pedantic in regards to Python, yes you're right.
- jolux 4y agoProbably the influence of the ML module system, where modules are per-file and module interfaces are in a separate file.
- pjmlp 4y agoC and C++ aren't the outlier, because many projects use translation units as base_os_arch.extension. And in what regards C++, we have proper modules now, which also don't map into a specific file. Java now has a mix of packages and modules, because not all packages should be public to start with. Ada introduced a similar concept in 1983. C++ modules builds on similar ideas, with module partitions. Then we have plenty of other less mainstream languages.
- ibraheemdev 4y agoThe most common confusion with this is that when importing library code from your binary, you use `crate_name::...` as opposed to `crate::...`, as they are separate. You also don't have to declare the lib.rs as a module from the binary, and generally all your `mod` statements will reside in your lib.rs
- faitswulff 4y agoHey, your comment made me realize I'm an idiot and I was trying to make a lib folder at the sibling level to the src folder. That plus your tip about how to organize the files and everything makes sense now. Thanks!
- ww520 4y agoAgree. Module is a weak design of Rust.
- mynameisash 4y agoAfter 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.
- rackjack 4y agoI 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. So when you try to move a module into its own file, you need to keep the `mod foo` in the original file.
- pjmlp 4y agoAda, with foo; -- Import use foo; -- open namespace Seems familiar.
- 8K832d7tNmiQ 4y agoimporting a module is the most fustrating part of writing rust code next to writing a multiple-spawn tasks. Can someone help me how to import a module in lib/m.rs from lib.rs ? I've never seen someone asked about it.
- duped 4y agoFolder structure src/lib.rs lib/ mod.rs m.rs In lib.rs use lib::m; In lib/mod.rs pub mod m; You need the mod.rs file in subdirectories.
- deleted 4y ago[deleted]
- bobbylarrybobby 4y agoRust has two distinct semantics surrounding how items can be "in scope". First, the `mod` keyword takes a module that the current file/scope can see and makes it available within that file/scope as if it had been defined there. This is necessary to make the module's items accessible, i.e., specifiable by their path within the module, in the current file/scope. Then the `use` keyword takes an already-accessible member of a named scope -- a module, a type, etc -- and makes it usable by name (i.e., without scoping on each use) in the current scope.
- duped 4y agoIMHO 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.
- deleted 4y ago[deleted]