Skip to content
This repository has been archived by the owner on May 5, 2019. It is now read-only.

Commit

Permalink
translate 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Aug 22, 2018
1 parent 02c835a commit 9c6b6a0
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 148 deletions.
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
- [Cargo 和 crates.io](rust-2018/cargo-and-crates-io/index.md)
- [`cargo check` 用以快速检查](rust-2018/cargo-and-crates-io/cargo-check-for-faster-checking.md)
- [`cargo install` 自动安装工具](rust-2018/cargo-and-crates-io/cargo-install-for-easy-installation-of-tools.md)
- [`cargo new` 自动创建可执行项目](rust-2018/cargo-and-crates-io/cargo-new-defaults-to-a-binary-project.md)
- [`cargo new` 创建可执行项目](rust-2018/cargo-and-crates-io/cargo-new-defaults-to-a-binary-project.md)
- [`cargo rustc` 用于传递标记至 `rustc`](rust-2018/cargo-and-crates-io/cargo-rustc-for-passing-arbitrary-flags-to-rustc.md)
- [Cargo workspaces 用于拥有多包的项目](rust-2018/cargo-and-crates-io/cargo-workspaces-for-multi-package-projects.md)
- [多文件的 `examples`](rust-2018/cargo-and-crates-io/multi-file-examples.md)
- [`patch` 替换依赖](rust-2018/cargo-and-crates-io/replacing-dependencies-with-patch.md)
- [Cargo 可以使用本地包库](rust-2018/cargo-and-crates-io/cargo-can-use-a-local-registry-replacement.md)
- [Cargo 更改源](rust-2018/cargo-and-crates-io/cargo-can-use-a-local-registry-replacement.md)
- [Crates.io 不允许使用通配符](rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.md)
- [文档](rust-2018/documentation/index.md)
- [新版"the book"](rust-2018/documentation/new-editions-of-the-book.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Cargo can use a local registry replacement
# Cargo 更改源

![Minimum Rust version: 1.12](https://img.shields.io/badge/Minimum%20Rust%20Version-1.12-brightgreen.svg)

Cargo finds its packages in a "source". The default source is [crates.io](https://crates.io). However, you
can choose a different source in your `.cargo/config`:
Cargo 在“源”中找到它的包。默认源是 [crates.io](https://crates.io)。但是,您可以在 `.cargo/config` 中选择其他源:

```toml
[source.crates-io]
Expand All @@ -13,26 +12,13 @@ replace-with = 'my-awesome-registry'
registry = 'https://github.com/my-awesome/registry-index'
```

This configuration means that instead of using crates.io, Cargo will query
the `my-awesome-registry` source instead (configured to a different index
here). This alternate source *must be the exact same* as the crates.io index.
Cargo assumes that replacement sources are exact 1:1 mirrors in this respect,
and the following support is designed around that assumption.

When generating a lock file for crate using a replacement registry, the
original registry will be encoded into the lock file. For example in the
configuration above, all lock files will still mention crates.io as the
registry that packages originated from. This semantically represents how
crates.io is the source of truth for all crates, and this is upheld because
all replacements have a 1:1 correspondance.

Overall, this means that no matter what replacement source you're working
with, you can ship your lock file to anyone else and you'll all still have
verifiably reproducible builds!

This has enabled tools like
[`cargo-vendor`](https://github.com/alexcrichton/cargo-vendor) and
[`cargo-local-registry`](https://github.com/alexcrichton/cargo-local-registry),
which are often useful for "offline builds." They prepare the list of all
Rust dependencies ahead of time, which lets you ship them to a build machine
with ease.
这种配置意味着不是使用 crates.io,而是 Cargo 将查询 `my-awesome-registry` 源(在此处配置为不同的索引)。此备用源 *必须与 crates.io 索引完全相同*
Cargo 假设替换源在这方面是精确的 1:1 镜像,并且围绕该假设设计了以下支持。

使用替换源为 crate 生成锁定文件时,原始源将编码到锁定文件中。例如,在上面的配置中,所有锁定文件仍然会提到 crates.io 作为包源的信息。
这在语义上代表了 crates.io 如何成为所有 crates 的真实来源,并且由于所有替换都具有 1:1 的对应性,因此这是坚持的。

总的来说,这意味着无论你使用何种替换源,您都可以将锁文件发送给其他任何人,并且你仍然可以获得可验证的可重现的构建!

这个工具有 [`cargo-vendor`](https://github.com/alexcrichton/cargo-vendor)[`cargo-local-registry`](https://github.com/alexcrichton/cargo-local-registry),
这对于“离线构建”通常很有用。他们提前准备所有 Rust 依赖项的列表,这使您可以轻松地将它们发送到构建计算机。
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
# `cargo check` for faster checking
# `cargo check` 用于快速检查

![Minimum Rust version: 1.16](https://img.shields.io/badge/Minimum%20Rust%20Version-1.16-brightgreen.svg)

`cargo check` is a new subcommand should speed up the development
workflow in many cases.

What does it do? Let's take a step back and talk about how `rustc` compiles
your code. Compilation has many "passes", that is, there are many distinct
steps that the compiler takes on the road from your source code to producing
the final binary. However, you can think of this process in two big steps:
first, `rustc` does all of its safety checks, makes sure your syntax is
correct, all that stuff. Second, once it's satisfied that everything is in
order, it produces the actual binary code that you end up executing.

It turns out that that second step takes a lot of time. And most of the time,
it's not neccesary. That is, when you're working on some Rust code, many
developers will get into a workflow like this:

1. Write some code.
2. Run `cargo build` to make sure it compiles.
3. Repeat 1-2 as needed.
4. Run `cargo test` to make sure your tests pass.
5. Try the binary yourself
6. GOTO 1.

In step two, you never actually run your code. You're looking for feedback
from the compiler, not to actually run the binary. `cargo check` supports
exactly this use-case: it runs all of the compiler's checks, but doesn't
produce the final binary. To use it:
`cargo check` 是一个新的子命令,可以在很多情况下加快开发工作流程。

它有什么作用?让我们退一步说,讨论 `rustc` 如何编译代码。编译有许多“过程”,也就是说,编译器在从源代码到生成最终二进制文件的过程中有许多不同的步骤。
但是,您可以通过两个重要步骤来考虑这个过程:首先,`rustc` 执行所有安全检查,确保您的语法正确,所有这些。其次,一旦满足一切顺序,就会生成最终执行的实际二进制代码。

事实证明,第二步需要花费很多时间。而且大多数时候,这不是必要的。也就是说,当您处理一些 Rust 代码时,许多开发人员将进入这样的工作流程:

1. 写一些代码。
2. 运行 `cargo build` 以确保它编译。
3. 根据需要重复1-2。
4. 运行 `cargo test` 以确保测试通过。
5. 亲自尝试二进制文件
6. GOTO 1。

在第二步中,您实际上从未运行过您的代码。您正在寻找编译器的反馈,而不是实际运行二进制文件。 `cargo check` 正好支持这个用例:它运行所有编译器的检查,但不生成最终的二进制文件。要使用它:

```console
$ cargo check
```

where you may normally `cargo build`. The workflow now looks like:

1. Write some code.
2. Run `cargo check` to make sure it compiles.
3. Repeat 1-2 as needed.
4. Run `cargo test` to make sure your tests pass.
5. Run `cargo build` to build a binary and try it yourself
6. GOTO 1.
在那里你通常可以 `cargo build`。 工作流现在看起来像:

1. 写一些代码。
2. 运行`cargo check`以确保它编译。
3. 根据需要重复1-2。
4. 运行`cargo test`以确保测试通过。
5. 运行`cargo build`来构建二进制文件并自己尝试
6. GOTO 1。

So how much speedup do you actually get? Like most performance related
questions, the answer is "it depends." Here are some very un-scientific
benchmarks at the time of writing.
那么你实际获得了多少加速?与大多数相关的问题一样,答案是“它取决于”。在撰写本文时,这里有一些非科学的基准。

| build | performance | check performance | speedup |
|--------|-------------|-------------------|---------|
| initial compile | 11s | 5.6s | 1.96x |
| second compile (no changes) | 3s | 1.9s | 1.57x |
| third compile with small change | 5.8s | 3s | 1.93x |
| third compile with small change | 5.8s | 3s | 1.93x |
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
# `cargo install` for easy installation of tools
# `cargo install` 用于自动安装

![Minimum Rust version: 1.5](https://img.shields.io/badge/Minimum%20Rust%20Version-1.5-brightgreen.svg)

Cargo has grown a new `install` command. This is intended to be used for installing
new subcommands for Cargo, or tools for Rust developers. This doesn't replace the need
to build real, native packages for end-users on the platforms you support.
Cargo 已经发展了一种新的 `install` 命令。 这旨在用于为 Cargo 安装新的子命令,或者为 Rust 开发人员安装工具。
这并不能取代为您支持的平台上的最终用户构建真实的本机程序包的需要。

For example, this guide is created with [`mdbook`](https://crates.io/crates/mdbook). You
can install it on your system with
例如,本指南是使用 [`mdbook`](https://crates.io/crates/mdbook)。 你可以将它安装在你的系统上:

```console
$ cargo install mdbook
```

And then use it with
然后使用:

```console
$ mdbook --help
```

As an example of extending Cargo, you can use the [`cargo-update`](https://crates.io/crates/cargo-update)
package. To install it:
作为扩展 Cargo 的示例,你可以使用[`cargo-update`](https://crates.io/crates/cargo-update)包。 要安装它:

```console
$ cargo install cargo-update
```

This will allow you to use this command, which checks everything you've `cargo install`'d and
updates it to the latest version:
这将允许你使用此命令,该命令检查你 `cargo install` 的所有内容并将其更新为最新版本:

```console
$ cargo install-update -a
```
```
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
# `cargo new` defaults to a binary project
# `cargo new` 创建一个默认可执行项目

![Minimum Rust version: 1.25](https://img.shields.io/badge/Minimum%20Rust%20Version-1.25-brightgreen.svg)

`cargo new` will now default to generating a binary, rather than a library.
We try to keep Cargo’s CLI quite stable, but this change is important, and is
unlikely to cause breakage.
`cargo new` 现在默认生成二进制文件,而不是库。我们试图保持 Cargo 的 CLI 非常稳定,但这种变化很重要,不太可能导致破损。

For some background, cargo new accepts two flags: `--lib`, for creating
libraries, and `--bin`, for creating binaries, or executables. If you don’t
pass one of these flags, it used to default to `--lib`. At the time, we made
this decision because each binary (often) depends on many libraries, and so
we thought the library case would be more common. However, this is incorrect;
each library is depended upon by many binaries. Furthermore, when getting
started, what you often want is a program you can run and play around with.
It’s not just new Rustaceans though; even very long-time community members
have said that they find this default surprising. As such, we’ve changed it,
and it now defaults to `--bin`.
对于某些背景,cargo new 接受两个标志: `--lib` 用于创建库,`--bin` 用于创建二进制文件或可执行文件。 如果你没有传递其中一个标志,它曾经默认为 `--lib`
当时,我们做出了这个决定,因为每个二进制文件(通常)都依赖于许多库,因此我们认为库案例会更常见。但是,这是不正确的; 每个包都依赖于许多二进制文件。
此外,在开始使用时,你经常需要的是一个可以运行和使用的程序。而且,不仅仅是新 Rustaceans们, 甚至是很长时间的社区成员都说他们发现这个默认值令人惊讶。
因此,我们已经改变它,它现在默认为 `--bin`
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
# `cargo rustc` for passing arbitrary flags to rustc
# `cargo rustc` 用于传递标记至 rustc

![Minimum Rust version: 1.1](https://img.shields.io/badge/Minimum%20Rust%20Version-1.1-brightgreen.svg)

`cargo rustc` is a new subcommand for Cargo that allows you to pass arbitrary
`rustc` flags through Cargo.
`cargo rustc` 是Cargo的一个新的子命令,它允许你通过 cargo 传递任意标记到 `rustc`

For example, Cargo does not have a way to pass unstable flags built-in. But
if we'd like to use `print-type-sizes` to see what layout information our
types have. We can run this:
例如,Cargo 没有办法传递内置的不稳定标志。但是如果我们想使用 `print-type-sizes` 来查看我们的类型有哪些布局信息。我们可以运行这个:

```console
$ cargo rustc -- -Z print-type-sizes
```

And we'll get a bunch of output describing the size of our types.
我们将得到一堆描述我们类型大小的输出。

## Note

`cargo rustc` only passes these flags to invocations of your crate, and not to any `rustc`
invocations used to build dependencies. If you'd like to do that, see `$RUSTFLAGS`.
## 注意
`cargo rustc` 只会将这些标记传递给你的 crate 的调用,而不是用于构建依赖项的任何 `rustc` 调用。
如果你想这样做,请参阅 `$RUSTFLAGS`
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
# Cargo workspaces for multi-package projects
# Cargo workspaces 用于有多个子包的项目

![Minimum Rust version: 1.12](https://img.shields.io/badge/Minimum%20Rust%20Version-1.12-brightgreen.svg)

Cargo used to have two levels of organization:
Cargo 曾经有两个组织层次:

* A *package* contains one or more crates
* A crate has one or more modules
* 一个 *package* 有一个或多个 crates
* 一个 *crate* 有一个或多个 modules

Cargo now has an additional level:
Cargo 现在有一个额外的层次:

* A *workspace* contains one or more packages
* 一个 *workspace* 包含一个或多个 packages

This can be useful for larger projects. For example, [the `futures`
respository] is a *workspace* that contains many related packages:
[the `futures` package]: https://github.com/rust-lang-nursery/futures-rs

这对于大型项目非常有用。例如,[the `futures` package] 是一个 *workspace*,包含许多相关的包:

* futures
* futures-util
* futures-io
* futures-channel

and more.

[the `futures` package]: https://github.com/rust-lang-nursery/futures-rs
还有其他。

Workspaces allow these packages to be developed individually, but they share
a single set of dependencies, and therefore have a single target directory
and a single `Cargo.lock`.
Workspaces 允许单独开发这些包,但它们共享一组依赖项,因此只有单个 target 目录和单个 `Cargo.lock`

For more details about workspaces, please see [the Cargo documentation](https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-workspace-section).
更多有关 workspaces, 请查阅 [the Cargo documentation](https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-workspace-section).
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
# Crates.io disallows wildcard dependencies
# Crates.io 不允许使用通配符

![Minimum Rust version: 1.6](https://img.shields.io/badge/Minimum%20Rust%20Version-1.6-brightgreen.svg)

Crates.io will not allow you to upload a package with a wildcard dependency.
In other words, these:
Crates.io 不允许您上传具有通配符依赖关系的包。 换句话说,这些:

```toml
[dependencies]
regex = "*"
```

A wildcard dependency means that you work with any possible version of your
dependency. This is highly unlikely to be true, and would cause unnecessary
breakage in the ecosystem.
通配符依赖性意味着您可以使用任何可能的依赖项版本。 这极不可能是真实的,并且会在生态系统中造成不必要的破坏。

Instead, depend on a version range. For example, `^` is the default, so
you could use
相反,取决于版本范围。例如,`^`是默认值,因此您可以使用:

```toml
[dependencies]
regex = "1.0.0"
```

instead. `>`, `<=`, and all of the other, non-`*` ranges work as well.
相应的, `>`, `<=`, 和所有其他的非`*`范围都是可以的。
10 changes: 3 additions & 7 deletions src/rust-2018/cargo-and-crates-io/multi-file-examples.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Multi-file examples
# 多个演示用例

![Minimum Rust version: 1.22](https://img.shields.io/badge/Minimum%20Rust%20Version-1.22-brightgreen.svg)

Cargo has an `examples` feature for showing people how to use your package.
By putting individual files inside of the top-level `examples` directory, you
can create multiple examples.
Cargo有一个 `examples` 功能,用于向人们展示如何使用您的包裹。通过将单个文件放在顶级 `examples` 目录中,您可以创建多个示例。

But what if your example is too big for a single file? Cargo supports adding
sub-directories inside of `examples`, and looks for a `main.rs` inside of
them to build the example. It looks like this:
但是如果你的例子对于单个文件来说太大了怎么办?Cargo支持在 `examples` 中添加子目录,并在其中查找 `main.rs` 来构建示例。它看起来像这样:

```text
my-package
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
# Replacing dependencies with patch
# patch 替换依赖

![Minimum Rust version: 1.21](https://img.shields.io/badge/Minimum%20Rust%20Version-1.21-brightgreen.svg)

The `[patch]` section of your `Cargo.toml` can be used when you want to
override certain parts of your dependency graph.
当你想要覆盖依赖图的某些部分时,可以使用你的 `Cargo.toml``[patch]` 部分。

> Cargo has a `[replace]` feature that is similar; while we don't intend to deprecate
> or remove `[replace]`, you should prefer `[patch]` in all circumstances.
> cargo 有一个类似的 `[replace]` 功能; 虽然我们不打算弃用或删除 `[replace]`,但在任何情况下都应该更喜欢 `[patch]`
So what’s it look like? Let’s say we have a Cargo.toml that looks like this:
那么它看起来像什么? 假设我们有一个看起来像这样的 Cargo.toml

```toml
[dependencies]
foo = "1.2.3"
```

In addition, our `foo` package depends on a `bar` crate, and we find a bug in `bar`.
To test this out, we’d download the source code for `bar`, and then update our
`Cargo.toml`:
另外,我们的 `foo` 包依赖于 `bar` 包,我们在 `bar` 中发现了一个错误。为了测试这个,我们下载了 `bar` 的源代码,然后更新我们的 `Cargo.toml`

```toml
[dependencies]
Expand All @@ -27,9 +23,6 @@ foo = "1.2.3"
bar = { path = '/path/to/bar' }
```

Now, when you `cargo build`, it will use the local version of `bar`, rather
than the one from crates.io that `foo` depends on. You can then try out your
changes, and fix that bug!
现在,当你 `cargo build` 时,它将使用本地版本的 `bar`,而不是来自 crates.io 的 `foo` 所依赖的版本。然后,您可以尝试更改,并修复该错误!

For more details, see [the documentation for
`patch`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section).
更多细节,查阅 [the documentation for `patch`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section).

0 comments on commit 9c6b6a0

Please sign in to comment.