Is a static in lib.rs unresolvable

I think the confusion comes from the term crate, package and target.

rustc compiles crates, each crate has a root module, which is the source file that gets passed to rustc to compile the crate.

cargo manages packages and builds targets: a package can have multiple targets, and each of which is a separate crate alone.

it could be confusing because casually, when people say "use the foo crate", they are most likely refering to "use the foo package from crates.io".

it's more confusing, because there are crate types, and there are also cargo target types.

a crate type can be lib, rlib, cdylib, bin, proc-macro, etc, and a target type can be one of lib, bin, example, test, bench.

the lib target of a cargo package is special, you can only have at most one of lib target, which, by default, has the same target name (not necessarily crate name, see caveat below) as the package name, and uses src/lib.rs as its crate root. so, people usually say "the foo crate" in short for "the foo lib crate in the foo package (from crates.io)".

a caveat: crate names cannot use hyphens, they must be a valid rust identifier, but cargo package name and target names can, and do use hyphens, so cargo will translate hyphens in target names into underscores for crate names. though this difference is only relevant for the lib target/crate, which needs to be imported by other crates, for other targets, cargo uses target names, not their corresponding crate names.

3 Likes