I am a little confused as to the exact things hosted on crates.io (is a 'crate' the proper way to refer to those)? My understanding is that a crate is a unit of compilation in Rust, but then what is the mapping between crates and what is on crates.io? For instance, The Rust Programming Language appendix on macros says that since there can only be one procedural macro per crate:
Our two crates are tightly related, so we create the procedural macro crate within the directory of our
hello_macro
crate. If we change the trait definition inhello_macro
, we’ll have to change the implementation of the procedural macro inhello_macro_derive
as well. The two crates will need to be published separately, and programmers using these crates will need to add both as dependencies and bring them both into scope. We could instead have the hello_macro crate usehello_macro_derive
as a dependency and reexport the procedural macro code. But the way we’ve structured the project makes it possible for programmers to usehello_macro
even if they don’t want thederive
functionality.
It has to be published separately on crates.io. This seems pretty clear: a crate on crates.io is the same as a local crate, and the mapping is one-to-one.
However, when discussing projects with both an executable and a library, it implies that they are separate crates but needn't be published separately. For instance, the sccache repo has both main.rs and lib.rs. Is the separate binary crate not actually stored on crates.io and resides in the repo only? Then how does cargo install figure out what to install?
What is a "package"?
I tried to run cargo package
with a sample project that contains both binary and library targets. And both were added to the .cargo file (by the way, is the exact format of .cargo archives documented anywhere?). This still leaves me confused. Can we publish multiple crates as a part of one package? Should we then refer to what is stored on crates.io as packages? Am I right to assume that each package can contain multiple binary crates but only one library crate? This is my current understanding.