Skip to content

New guide: crates and modules #17555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2014
Merged

Conversation

steveklabnik
Copy link
Member

This is an almost-done draft of a guide on crates and modules. This is a hard guide to get right, I had to remove a chunk of the Guide because it was confusing.

I've also pushed up https://github.com/steveklabnik/phrases which has matching code. Whenever we finish this guide, I think it'd be good to have a sample crate like this in the rust-lang org for people to compare against. The hardest part of a guide like this is that it depends on multiple files being correct, and being able to point to a repository would be very helpful.

Things yet to do:

  1. external crates via cargo
  2. documentation

I'm super open to still revising this if it's still confusing. There's been a lot of Reddit discussion about the module system, and I tried to incorporate those posts and the comments into this.

@steveklabnik
Copy link
Member Author

Oh, and paralell compilation via splitting things up into multiple crates needs to be added.

@@ -0,0 +1,544 @@
% The Rust Crates and Modules Guide
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only read one sentence, and I'm already excited!

@alexcrichton
Copy link
Member

🌴

Rust has two distinct terms that relate to the module system: "crate" and
"module." A crate is synonymous with a 'library' or 'package' in other
languages. Hence "Cargo" as the name of Rust's package management tool: you
ship your crates to others with Cargo. Each crate is a single compilation unit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A definition of "compilation unit" would be good here. If I understood the following paragraphs correctly, it seems to be a bit different from translation unit or C#'s use of the term.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed.

@charlieflowers
Copy link

This helped me a lot. However, I was still confused when trying to "cargo-ize" a project I've been working on for a while. I could not figure out how to let module A use module B, and to let my lib.rs use them both.

Then I came across this statement in an online discussion, and everything became clear. I'm sharing it here in case it helps you capture and squash forever the source of my confusion: "You compile a crate, not files. foo.rs is just mod foo { ... } factored out. foo/bar.rs is just mod foo { mod bar {...} }. They have no meaning without the crate root."

@charlieflowers
Copy link

Let me clarify the confusion I have now resolved, in the hopes it can improve the guide. I was coming from a C# mindset (I imagine Java would be similar). If I have module A that uses module B, and I want my library to use both of them, then why can't module A say, within its own source file, to import module B? In fact, if I start out by coding module A and then module B (but not the library using them yet), that approach is exactly right: module A will say "mod moduleB;".

But once I make a library, I have to remove that line from module A. Instead, my lib.rs will say "mod moduleA;" and "mod moduleB;". At first, this feels entirely wrong to a C# developer because, if module A depends on module B, it should simply be able to say so!

The reason this is confusing from a C# mindset is that C# developers are not used to explicitly stating what they want inside of a "crate." They don't have to think about a crate concept at all. If my C# exe needs to use library Foo, and Foo uses Bar, I don't have to say anything about Bar in my exe.

But in Rust, in my "crate file," I must explicitly state all the things that will be included in the crate. Once I got that point, everything was clear to me. (I'm sure there are nuances I'm unaware of ... maybe this comment will help you capture and elucidate them too).

I assume Rust wants you to explicitly state what's in your crate in order to give you greater control, and also probably so you don't have the C++ "headers being loaded multiple times" problem. But that might be something to address too.

@renato-zannon
Copy link
Contributor

But once I make a library, I have to remove that line from module A. Instead, my lib.rs will say "mod moduleA;" and "mod moduleB;". At first, this feels entirely wrong to a C# developer because, if module A depends on module B, it should simply be able to say so!

I'm a Ruby developer, and I second this confusion. It took me a lot of head scratching before I stopped trying to simulate require using mod foo;.

@aturon
Copy link
Member

aturon commented Oct 17, 2014

What's the status on this PR? Good to go?

@steveklabnik
Copy link
Member Author

@aturon the conventions question is still open, and i should either remove the header or fill out the 'documenting crates' section. What do you think?

@steveklabnik
Copy link
Member Author

I've squashed this up, and as long as we're okay with the convention, I think it's good to go.

@alexcrichton
Copy link
Member

Looks good to me, two minor nits and otherwise r=me:

  • Would you be ok leaving out the multi-line import for now? We can add it back in once we have a convention around it (and it seems like it's a kinda just tacked on anyway right now)
  • Can you run rustdoc --test over this guide? I think some examples will need to be marked with ignore.

@steveklabnik
Copy link
Member Author

how embarrassing 😦 😓 😅

fixed. :)

bors added a commit that referenced this pull request Oct 21, 2014
This is an almost-done draft of a guide on crates and modules. This is a hard guide to get right, I had to remove a chunk of the Guide because it was confusing.

I've also pushed up https://github.com/steveklabnik/phrases which has matching code. Whenever we finish this guide, I think it'd be good to have a sample crate like this in the rust-lang org for people to compare against. The hardest part of a guide like this is that it depends on multiple files being correct, and being able to point to a repository would be very helpful.

Things yet to do:

1. external crates via cargo
2. documentation

I'm super open to still revising this if it's still confusing. There's been a lot of Reddit discussion about the module system, and I tried to incorporate those posts and the comments into this.
@bors bors closed this Oct 21, 2014
@bors bors merged commit 8851ec6 into rust-lang:master Oct 21, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 11, 2024
internal: Inline generated syntax methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.