Why can a Cargo package only have one library target?

后端 未结 2 1145
旧时难觅i
旧时难觅i 2020-12-11 15:20

According to its manual, Cargo packages can have multiple executable targets, but only one library target is allowed.

A package can contain zero or o

相关标签:
2条回答
  • 2020-12-11 16:03

    I would expect that it is because a library crate within a package is already a collection (functions, types, values, etc.) whereas the binary crates are opaque executables (with a main entry point). Consequently, the library crates name is merely the root module within its hierarchy and the binary crates name is its entirety and occur at the top level of the package.

    0 讨论(0)
  • 2020-12-11 16:10

    Cargo is primarily a package manager. Thus, the primary role of a package is to define a library.

    When we use a crate as a dependency, we only specify the package name in our Cargo.toml. Since there can be at most one library, Cargo doesn't need you to specify which one to use. If it were allowed to define multiple libraries in the same package, then we'd need to specify a way to define dependencies between them, so you'd have two ways to declare dependencies (external packages vs. internal crates), making the system more complex.

    On the other hand, adding a dependency that doesn't provide a library doesn't make sense, at least not with Cargo, since Cargo only cares about the library target in that context. Thus, there is no reason to limit the other types of targets (binaries, examples, tests, etc.) to one each.

    0 讨论(0)
提交回复
热议问题