Cargo: FAQ docs request: What is the distinction between 'crate' and 'package'?

Created on 9 Dec 2016  路  8Comments  路  Source: rust-lang/cargo

I spoke with @steveklabnik a couple nights ago and they informed that the terms 'crate' and 'package' are different concepts in the context of cargo. Someone in the #rust-tools channel on IRC just asked what the differences are. It might be worth clarifying this in the Frequently Asked Questions section (or maybe a different section?).

I'd be happy to write-up something for this, but I actually don't recall the distinctions from the conversation :)

A-documenting-cargo-itself

Most helpful comment

Dropping a quick info for others users following the first Google hit for "cargo crate vs package"...

(... the doc request has been closed without comment, the link to the FAQ doesn't help, the discussion is a bit confusing, and now we even have workspaces.)


The Rust Guide contains a great summary:

Cargo used to have two levels of organization:

  • A package contains one or more crates
  • A crate has one or more modules

Cargo now has an additional level:

  • A workspace contains one or more packages

All 8 comments

For reference, the questions the user asked on IRC:

terminology question: what is the difference between a "crate" and a "package"?
`cargo metadata` list `packages` for instance, and Cargo.toml has a `package` section as well
but rustc only builds crates (--crate-type, --crate-name), and in Rust files you also declare "extern crate foo"
and it they refer to the same thing: the "foo" crate will be listed as a "package" by `cargo metadata`
is there a subtle distinction?

It's confusing because we don't often use the terms in a clear way.

A cargo.toml is associated with a package, which is why it has [package] at the top. A package can have 0-1 library and 0-n binary crates, minimum 1.

So the package is the "source" from which crates are created? And when I say "extern crate foo" in a Rust file, I'm actually referring to the rlib crate that's built by cargo after downloading the foo package from crates.io because I added the dependency on that package in the Cargo.toml?

So, when I choose a target with cargo build --bin foo, am I building the binary crate "foo"? Are targets for cargo the same as crates? Or can there be targets that do not end up building crates?

(I was the one who asked the question on IRC).

So, when I choose a target with cargo build --bin foo, am I building the binary crate "foo"?

Yes.

Are targets for cargo the same as crates?

Yes. Library, binaries, exercises, integration tests all are different targets/crates.

So the package is the "source" from which crates are created?

Yes. It's a Cargo.toml plus the Rust source code.

And when I say "extern crate foo" in a Rust file, I'm actually referring to the rlib crate that's built by cargo after downloading the foo package from crates.io because I added the dependency on that package in the Cargo.toml?

Yes. Cargo downloads the package named foo from crates.io, builds its library target (crate) and wires it in to the corresponding extern crate declaration. An interesting thing to note that crate names (foo) are controlled by cargo during the build process. That is, the binary for the library does not know its name at all.

@steveklabnik @matklad Okay thank you. I think I am less confused now ;)

So, the *.crate files on crates.io contain the source for Cargo packages, not Cargo crates?

Well both. That is, packages contain one or more crates. The Cargo.toml contains configuration for the whole package, under [package], and then for each specific crate under [lib] or [[bin]].

Dropping a quick info for others users following the first Google hit for "cargo crate vs package"...

(... the doc request has been closed without comment, the link to the FAQ doesn't help, the discussion is a bit confusing, and now we even have workspaces.)


The Rust Guide contains a great summary:

Cargo used to have two levels of organization:

  • A package contains one or more crates
  • A crate has one or more modules

Cargo now has an additional level:

  • A workspace contains one or more packages
Was this page helpful?
0 / 5 - 0 ratings