TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Learning Rust – Day 4 – Understanding Modules

61 pointsby dadeabout 3 years ago

6 comments

mynameisashabout 3 years 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&#x2F;mod.rs with foo&#x2F;bar.rs).<p>It pretty much instantly became obvious how to use them, and in retrospect, I don&#x27;t know why it was so confusing. I think it&#x27;s a good example of how important it is to [a] apply deliberate practice&#x2F;learning and [b] have empathy for others that haven&#x27;t (yet) progressed to our own level of understanding.
评论 #31358294 未加载
评论 #31359395 未加载
faitswulffabout 3 years ago
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.
评论 #31355027 未加载
评论 #31354135 未加载
评论 #31356370 未加载
dupedabout 3 years ago
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.
rackjackabout 3 years ago
I think the most surprising thing about Rust modules is that you need to &quot;&quot;&quot;pre-declare&quot;&quot;&quot; them, especially since Rust doesn&#x27;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.
评论 #31363711 未加载
8K832d7tNmiQabout 3 years ago
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&#x2F;m.rs from lib.rs ? I&#x27;ve never seen someone asked about it.
评论 #31359972 未加载
评论 #31358374 未加载
评论 #31357613 未加载
brianhorakhabout 3 years ago
Der Weg ist das Ziel!
评论 #31360117 未加载