[英]What exactly is a 'crate' in the Cargo ecosystem and what is the mapping to what is on crates.io?
I am a little confused as to the exact things hosted on crates.io (is a 'crate' the proper way to refer to those)? 我对crates.io上托管的确切事物感到有点困惑(这是一个'crate'正确引用它们的方式)? 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?
我的理解是一个crate是Rust中的一个编译单元,但是crates和crates.io之间的映射是什么? For instance, The Rust Programming Language appendix on macros says that since there can only be one procedural macro per crate:
例如, 宏上的Rust编程语言附录说,因为每个箱子只能有一个程序宏:
Our two crates are tightly related, so we create the procedural macro crate within the directory of our
hello_macro
crate.我们的两个箱子紧密相关,因此我们在
hello_macro
箱子的目录中创建程序宏箱。 If we change the trait definition inhello_macro
, we'll have to change the implementation of the procedural macro inhello_macro_derive
as well.如果我们改变的特征定义
hello_macro
,我们将不得不更改程序宏的执行hello_macro_derive
为好。 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 use
hello_macro_derive
as a dependency and reexport the procedural macro code.我们可以让hello_macro crate使用
hello_macro_derive
作为依赖项并hello_macro_derive
过程宏代码。 But the way we've structured the project makes it possible for programmers to usehello_macro
even if they don't want thederive
functionality.但是我们构建项目的方式使得程序员可以使用
hello_macro
即使他们不想要derive
功能。
It has to be published separately on crates.io. 它必须在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.
这看起来非常清楚:crates.io上的箱子与本地箱子相同,并且映射是一对一的。
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.
例如,sccache repo有main.rs和lib.rs. Is the separate binary crate not actually stored on crates.io and resides in the repo only?
单独的二进制包是不是实际存储在crates.io上而只存在于repo中? 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. 我尝试使用包含二进制和库目标的示例项目来运行
cargo package
。 And both were added to the .cargo file (by the way, is the exact format of .cargo archives documented anywhere ?). 并且两者都被添加到.cargo文件中(顺便说一下,.cargo档案的确切格式是在任何地方记录的吗?)。 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 ?
那么我们应该将crates.io中存储的内容称为包吗? Am I right to assume that each package can contain multiple binary crates but only one library crate?
我是否正确地假设每个包可以包含多个二进制包,但只包含一个库包? This is my current understanding.
这是我目前的理解。
The exact things hosted on crates.io are crates inside packages. 在crates.io上托管的确切东西是包内的板条箱。
A crate is the output artifact of the compiler. crate是编译器的输出工件。
From Rust Reference Manual : 来自Rust参考手册 :
The compilation model centers on artifacts called crates.
编译模型以称为crates的工件为中心。 Each compilation processes a single crate in source form, and if successful, produces a single crate in binary form: either an executable or some sort of library.
每个编译以源代码形式处理单个包,如果成功,则以二进制形式生成单个包:可执行文件或某种类型的库。
A package is an artifact managed by Cargo, the Rust package manager. 包是由Rust包管理器Cargo管理的工件。
Cargo.toml
manifest file defines the package with the syntax: Cargo.toml
清单文件使用以下语法定义包:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
A package may contain one or more crates, for example one library crate, named as the package name and zero or more executable crates, each defined explicitly within a [[bin]]
section of the manifest file or implicitly if located inside the src/bin
directory of the package. 包可以包含一个或多个包,例如一个库包, 命名为包名和零个或多个可执行包 ,每个包在清单文件的
[[bin]]
部分中显式定义,或者如果位于src/bin
内部则隐式定义包的src/bin
目录。
The Cargo book uses the term crate as an alias for package. Cargo书使用术语crate作为包的别名。 Consider the following statement to try to get out some sense:
考虑以下语句以试图找出一些意义:
Generally* the main artifact of a package is a library crate, and since it is identified with the package name, it is customary to treat package and crate as synonyms. 通常*包的主要工件是库包,并且由于它是用包名标识的,因此习惯上将包和包作为同义词处理。
*: A package may contains only a binary, see for example ripgrep *:包可能只包含二进制文件,例如参见ripgrep
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.