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

Commit

Permalink
translate 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Aug 21, 2018
1 parent b0afaf2 commit b8e75d4
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 161 deletions.
10 changes: 5 additions & 5 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
## Rust 2018

- [Rust 2018](rust-2018/index.md)
- [Module system](rust-2018/module-system/index.md)
- [Raw identifiers](rust-2018/module-system/raw-identifiers.md)
- [Path clarity](rust-2018/module-system/path-clarity.md)
- [More visibility modifiers](rust-2018/module-system/more-visibility-modifiers.md)
- [Nested imports with `use`](rust-2018/module-system/nested-imports-with-use.md)
- [模块系统](rust-2018/module-system/index.md)
- [原始标识符](rust-2018/module-system/raw-identifiers.md)
- [路径清晰化](rust-2018/module-system/path-clarity.md)
- [更加可见的修饰符](rust-2018/module-system/more-visibility-modifiers.md)
- [`use` 进行导入嵌套](rust-2018/module-system/nested-imports-with-use.md)
- [Error handling and panics](rust-2018/error-handling-and-panics/index.md)
- [The `?` operator for easier error handling](rust-2018/error-handling-and-panics/the-question-mark-operator-for-easier-error-handling.md)
- [`?` in `main` and tests](rust-2018/error-handling-and-panics/question-mark-in-main-and-tests.md)
Expand Down
10 changes: 3 additions & 7 deletions src/rust-2018/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Rust 2018

The edition system was created for the release of Rust 2018. The theme of Rust 2018
is *productivity*. Rust 2018 improves upon Rust 2015 through new features,
simpler syntax in some cases, a smarter borrow-checker, and a host of other things.
These are all in service of the productivity goal. Rust 2015 was a foundation;
Rust 2018 smooths off rough edges, makes writing code simpler and easier,
and removes some inconsistencies.
该版本系统是为 Rust 2018 的发布而创建的. Rust 2018 的主题是*生产力*
Rust 2018 通过新功能,在某些情况下更简单的语法,更智能的借用检查器以及许多其他东西来改进 Rust 2015。
这些都是为了提高生产力目标。 Rust 2015 是一个基础; Rust 2018 使粗糙边缘平滑,使编写代码更简单,更容易,并消除了一些不一致性。
8 changes: 4 additions & 4 deletions src/rust-2018/module-system/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Module system
# 模块系统

[path clarity changes]: path-clarity.md
[路径清晰度变化]: path-clarity.md

In this chapter of the guide, we discuss a few changes to the module system.
The most notable of these are the [path clarity changes].
在本指南的这一章中,我们将讨论模块系统的一些变化。
其中最值得注意的是 [路径清晰度变化]
10 changes: 4 additions & 6 deletions src/rust-2018/module-system/more-visibility-modifiers.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# More visibility modifiers
# 更加可见的修饰符

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

You can use the `pub` keyword to make something a part of a module's public interface. But in
addition, there are some new forms:
您可以使用 `pub` 关键字将某些内容作为模块公共接口的一部分。 但此外,还有一些新形式:

```rust,ignore
pub(crate) struct Foo;
pub(in a::b::c) struct Bar;
```

The first form makes the `Foo` struct public to your entire crate, but not
externally. The second form is similar, but makes `Bar` public for one other
module, `a::b::c` in this case.
第一种形式使 `Foo` 结构公开在整个crate中,但不是外部的。
第二种形式是类似的,只在另一种模块 `a::b::c` 中,`Bar`是公开的。
10 changes: 5 additions & 5 deletions src/rust-2018/module-system/nested-imports-with-use.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Nested imports with `use`
# `use` 进行导入嵌套

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

A new way to write `use` statements has been added to Rust: nested import
groups. If you’ve ever written a set of imports like this:
在 Rust 中: 嵌套导入中添加了一种编写 `use` 语句的新方法。
如果您曾编写过这样的一组导入:

```rust
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
```

You can now write this:
可以这样写了:

```rust
# mod foo {
Expand All @@ -32,4 +32,4 @@ use std::{
# }
```

This can reduce some repetition, and make things a bit more clear.
这可以减少一些重复,并使事情更清晰。
170 changes: 60 additions & 110 deletions src/rust-2018/module-system/path-clarity.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,34 @@
# Path clarity
# 路径清晰化

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

The module system is often one of the hardest things for people new to Rust. Everyone
has their own things that take time to master, of course, but there's a root
cause for why it's so confusing to many: while there are simple and
consistent rules defining the module system, their consequences can feel
inconsistent, counterintuitive and mysterious.
对于刚接触 Rust 的人来说,模块系统通常是最困难的事情之一。
当然,每个人掌握东西的时间都不同,但是有一个根本原因,导致了为什么对许多人来说模块系统如此混乱:
尽管模块系统有了简单而一致的定义规则,但它们给人的感觉可能不一致,甚至是违反直觉的,神秘的。

As such, the 2018 edition of Rust introduces a few new module system
features, but they end up *simplifying* the module system, to make it more
clear as to what is going on.
因此,Rust 2018 引入了一些新的模块系统功能,它们将*简单化*模块系统,使其更加清晰。

Note: During the 2018 edition preview, there are two variants of the module
system under consideration, the "uniform paths" variant and the "anchored use
paths" variant. Most of these changes apply to both variants; the two variant
sections call out the differences between the two. We encourage testing of the
new "uniform paths" variant introduced in edition preview 2. The release of
Rust 2018 will stabilize one of these two variants and drop the other.
注意:在2018版预览期间,正在考虑的模块系统有两种变体,“统一路径(uniform paths)”变体和“锚定使用路径(anchored use paths)”变体。
这些变化大多数是适用于两种变体的; 两个变体部分也列出了两者之间的差异。
我们鼓励使用预览2版本的用户,引入新的“统一路径”变体。Rust 2018 的稳定发布时,将只会选择其一。

To test Rust 2018 with the new "uniform paths" variant, put
`#![feature(rust_2018_preview, uniform_paths)]` at the top of your `lib.rs` or
`main.rs`.
要使用新的“统一路径”变体测试 Rust 2018 ,请将 `#![feature(rust_2018_preview, uniform_paths)]` 放在 `lib.rs``main.rs` 的顶部。

Here's a brief summary:
这是一个简短的总结:

* `extern crate` is no longer needed
* The `crate` keyword refers to the current crate.
* Uniform paths variant: Paths work uniformly in both `use` declarations and in
other code. Paths work uniformly both in the top-level module and in
submodules. Any path may start with a crate, with `crate`, `super`, or
`self`, or with a local name relative to the current module.
* Anchored use paths variant: Paths in `use` declarations always start with a
crate name, or with `crate`, `super`, or `self`. Paths in code other than
`use` declarations may also start with names relative to the current module.
* A `foo.rs` and `foo/` subdirectory may coexist; `mod.rs` is no longer needed
when placing submodules in a subdirectory.
* `extern crate` 不再需要。
* `crate` 关键字指的是当前的 crate。
* 统一路径变体: 路径在 `use` 声明和其他代码中统一工作。路径在顶级模块和子模块中统一工作。任何路径都可以以crate开头,包括 `crate``super``self` ,或者具有相对于当前模块的本地名称。
* 锚定使用路径变量: `use` 声明中的路径始终以包名称开头,或者以 `crate``super``self` 开头。 除了 `use` 声明之外的代码中的路径也可以从相对于当前模块的名称开始。
* `foo.rs``foo /` 子目录可以共存; 将子模块放在子目录中时不再需要`mod.rs`

These may seem like arbitrary new rules when put this way, but the mental
model is now significantly simplified overall. Read on for more details!
这样看起来就像是新的规则,但现在心理模型整体上大大简化了。 请阅读以获得更多详情!

## More details
## 更多的细节
让我们依次讨论每个新功能。

Let's talk about each new feature in turn.

### No more `extern crate`

This one is quite straightforward: you no longer need to write `extern crate` to
import a crate into your project. Before:
### 不再需要 `extern crate`
这个非常简单:您不再需要编写 `extern crate` 来将crate导入到您的项目中。 之前:

```rust,ignore
// Rust 2015
Expand All @@ -59,7 +40,7 @@ mod submodule {
}
```

After:
现在:

```rust,ignore
// Rust 2018
Expand All @@ -69,42 +50,28 @@ mod submodule {
}
```

Now, to add a new crate to your project, you can add it to your `Cargo.toml`,
and then there is no step two. If you're not using Cargo, you already had to pass
`--extern` flags to give `rustc` the location of external crates, so you'd just
keep doing what you were doing there as well.

One other use for `extern crate` was to import macros; that's no longer needed.
Check [the macro section](../macros/macro-changes.html) for more.

### The `crate` keyword refers to the current crate.
现在,要为项目添加一个新的包,你可以将它添加到你的 `Cargo.toml`,然后没有第二步。
如果你没有使用Cargo,你必须通过 `--extern` 标志给 `rustc` 提供外部crate的位置,然后继续做其他的事前吧。

In `use` declarations and in other code, you can refer to the root of the
current crate with the `crate::` prefix. For instance, `crate::foo::bar` will
always refer to the name `bar` inside the module `foo`, from anywhere else in
the same crate.
`extern crate`的另一个用途是导入宏; 那也不再需要了。 查看[宏章节](../macros/macro-changes.html)以获取更多信息。

The prefix `::` previously referred to either the crate root or an external
crate; it now unambiguously refers to an external crate. For instance,
`::foo::bar` always refers to the name `bar` inside the external crate `foo`.
### `crate` 指当前crate.
`use` 声明和其他代码中,您可以使用 `crate::` 前缀来引用当前包的根。
例如,`crate::foo::bar` 将始终引用模块 `foo` 中的名称 `bar`,来自同一个crate中的任何其他位置。

### Uniform paths variant
前缀 `::` 以前称为crate root或外部crate; 它现在毫无疑问的是指外部crate。
例如,`::foo::bar`总是指外部crate中 `foo` 中的 `bar`

The uniform paths variant of Rust 2018 simplifies and unifies path handling
compared to Rust 2015. In Rust 2015, paths work differently in `use`
declarations than they do elsewhere. In particular, paths in `use`
declarations would always start from the crate root, while paths in other code
implicitly started from the current module. Those differences didn't have any
effect in the top-level module, which meant that everything would seem
straightforward until working on a project large enough to have submodules.
### 统一路径变体
与 Rust 2015 相比,Rust 2018 的统一路径变体简化并统一了路径处理。
在 Rust 2015 中,路径在 `use` 声明中的工作方式与在其他地方的工作方式不同。
特别地,`use` 声明中的路径总是从包根开始,而其他代码中的路径隐含地从当前模块开始。
这些差异在顶级模块中没有任何影响,这意味着在处理足够大的子模块项目之前,所有内容都会显得简单明了。

In the uniform paths variant of Rust 2018, paths in `use` declarations and in
other code always work the same way, both in the top-level module and in any
submodule. You can always use a relative path from the current module, a path
starting from an external crate name, or a path starting with `crate`, `super`,
or `self`.
在 Rust 2018 的统一路径变体中,`use` 声明和其他代码中的路径始终以相同的方式工作,无论是在顶级模块还是在任何子模块中。
您始终可以使用当前模块的相对路径,从外部包名称开始的路径,或以 `crate``super``self` 开头的路径。

Code that looked like this:
代码长这样:

```rust,ignore
// Rust 2015
Expand Down Expand Up @@ -135,9 +102,7 @@ fn func() {
}
}
```

will look exactly the same in Rust 2018, except that you can delete the `extern
crate` line:
在 Rust 2018 中看起来完全一样,除了删除 `extern crate` 行:

```rust,ignore
// Rust 2018 (uniform paths variant)
Expand Down Expand Up @@ -167,8 +132,7 @@ fn func() {
}
```

With Rust 2018, however, the same code will also work completely unmodified in
a submodule:
但是,使用 Rust 2018,相同的代码也可以在子模块中完全不修改:

```rust,ignore
// Rust 2018 (uniform paths variant)
Expand Down Expand Up @@ -200,21 +164,15 @@ mod submodule {
}
```

This makes it easy to move code around in a project, and avoids introducing
additional complexity to multi-module projects.

If a path is ambiguous, such as if you have an external crate and a local
module or item with the same name, you'll get an error, and you'll need to
either rename one of the conflicting names or explicitly disambiguate the path.
To explicitly disambiguate a path, use `::name` for an external crate name, or
`self::name` for a local module or item.
这样可以轻松地在项目中移动代码,并避免为引入多模块项目增加额外的复杂性。

### Anchored use paths variant
如果路径不明确,例如,如果您有外部包和本地模块或具有相同名称的项目,您将收到错误,并且您需要重命名其中一个冲突名称或明确消除路径歧义。
要明确消除路径歧义,请使用 `::name` 作为外部包名,或使用 `self::name` 作为本地模块或项目。

In the anchored use paths variant of Rust 2018, paths in `use` declarations
*must* begin with a crate name, `crate`, `self`, or `super`.
### 锚定使用路径
在 Rust 2018 的锚定使用路径变体中,`use` 声明 *必须* 必须以包名开头,开头包括 `crate` `self` `super`

Code that looked like this:
以前代码长这样:

```rust,ignore
// Rust 2015
Expand All @@ -230,7 +188,7 @@ mod foo {
use foo::Bar;
```

Now looks like this:
现在长这样:

```rust,ignore
// Rust 2018 (anchored use paths variant)
Expand All @@ -246,9 +204,7 @@ mod foo {
use crate::foo::Bar;
```

In addition, all of these path forms are available outside of `use`
declarations as well, which eliminates many sources of confusion. Consider this
code in Rust 2015:
此外,所有这些路径形式也可以在 `use` 声明之外使用,这消除了许多混淆的来源。 在Rust 2015中考虑以下代码:

```rust,ignore
// Rust 2015
Expand All @@ -275,16 +231,14 @@ mod submodule {
}
}
```
`futures` 示例中,`my_poll` 函数签名不正确,因为 `submodule` 不包含名为 `futures` 的项目; 也就是说,这条路径被认为是相对的。
但是因为 `use` 是锚定的,`use futures::` 即使单独的 `futures::` 也不行!
使用 `std` 它可能会更加令人困惑,因为你从来没有写过 `extern crate std;` 行。
那么为什么它在 `main` 中工作但不在子模块中工作?
同样的事情:它是一个相对路径,因为它不在 `use` 声明中。
`extern crate std;`被插入到crate root中,所以它在 `main` 中很好,但它根本不存在于子模块中。

In the `futures` example, the `my_poll` function signature is incorrect, because `submodule`
contains no items named `futures`; that is, this path is considered relative. But because
`use` is anchored, `use futures::` works even though a lone `futures::` doesn't! With `std`
it can be even more confusing, as you never wrote the `extern crate std;` line at all. So
why does it work in `main` but not in a submodule? Same thing: it's a relative path because
it's not in a `use` declaration. `extern crate std;` is inserted at the crate root, so
it's fine in `main`, but it doesn't exist in the submodule at all.

Let's look at how this change affects things:
让我们来看看这种变化如何影响:

```rust,ignore
// Rust 2018 (anchored use paths variant)
Expand Down Expand Up @@ -312,22 +266,18 @@ mod submodule {
}
```

Much more straightforward.
更加的直截了当。


### No more `mod.rs`
### 不再需要 `mod.rs`

In Rust 2015, if you have a submodule:
Rust 2015 中,子模块如下:

```rust,ignore
mod foo;
```

It can live in `foo.rs` or `foo/mod.rs`. If it has submodules of its own, it
*must* be `foo/mod.rs`. So a `bar` submodule of `foo` would live at
`foo/bar.rs`.
它可以是 `foo.rs` 或者 `foo/mod.rs`。如果是一个子模块,那么它 *必须* 有一个 `foo/mod.rs`。 这样的化, `bar` 存在于子模块 `foo` 中,将表现为 `foo/bar.rs`

In Rust 2018, `mod.rs` is no longer needed. `foo.rs` can just be `foo.rs`,
and the submodule is still `foo/bar.rs`. This eliminates the special
name, and if you have a bunch of files open in your editor, you can clearly
see their names, instead of having a bunch of tabs named `mod.rs`.
在 Rust 2018 中,`mod.rs` 不再需要,`foo.rs`仅仅表示 `foo.rs`,子模块仍然是 `foo/bar.rs`
这消除了特殊名称,如果你在编辑器中打开了一堆文件,你可以清楚地看到它们的名字,而不是有一堆名为 `mod.rs` 的标签。

0 comments on commit b8e75d4

Please sign in to comment.