包只能有一个库目标

包只能有一个库目标

本文介绍了为什么一个 Cargo 包只能有一个库目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据其手册,Cargo 包可以有多个可执行目标,但只允许一个库目标.

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

一个包可以包含零个或一个库包以及任意数量的二进制包.一个包中必须至少有一个 crate(库或二进制文件).

为什么只限一个?原因和好处是什么?

Why is it limited to one? What are the reasons and benefits?

推荐答案

Cargo 主要是一个包管理器.因此,包的主要作用是定义一个库.

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

当我们使用 crate 作为依赖项时,我们只在我们的 Cargo.toml 中指定包名称.由于最多可以有一个库,因此 Cargo 不需要您指定使用哪个库.如果允许在同一个包中定义多个库,那么我们需要指定一种方法来定义它们之间的依赖关系,因此您将有两种方法来声明依赖关系(外部包与内部 crate),使系统更复杂.

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.

另一方面,添加不提供库的依赖项没有意义,至少对于 Cargo 而言如此,因为 Cargo 只关心该上下文中的库目标.因此,没有理由将其他类型的目标(二进制文件、示例、测试等)限制为一种.

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.

这篇关于为什么一个 Cargo 包只能有一个库目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:30