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

Commit

Permalink
translate 3.3 & 3.4 & 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Aug 22, 2018
1 parent 815ac18 commit 534a54d
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 173 deletions.
18 changes: 9 additions & 9 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
- [`?``main` 和 tests中](rust-2018/error-handling-and-panics/question-mark-in-main-and-tests.md)
- [`std::panic` 控制崩溃](rust-2018/error-handling-and-panics/controlling-panics-with-std-panic.md)
- [中止崩溃](rust-2018/error-handling-and-panics/aborting-on-panic.md)
- [Control flow](rust-2018/control-flow/index.md)
- [Loops can `break` with a value](rust-2018/control-flow/loops-can-break-with-a-value.md)
- [`async`/`await` for easier concurrency](rust-2018/control-flow/async-await-for-easier-concurrency.md)
- [Trait system](rust-2018/trait-system/index.md)
- [`impl Trait` for returning complex types with ease](rust-2018/trait-system/impl-trait-for-returning-complex-types-with-ease.md)
- [`dyn Trait` for trait objects](rust-2018/trait-system/dyn-trait-for-trait-objects.md)
- [More container types support trait objects](rust-2018/trait-system/more-container-types-support-trait-objects.md)
- [Associated constants](rust-2018/trait-system/associated-constants.md)
- [Slice patterns](rust-2018/slice-patterns.md)
- [流程控制](rust-2018/control-flow/index.md)
- [循环可以停止并带有返回值](rust-2018/control-flow/loops-can-break-with-a-value.md)
- [`async`/`await` 早期并发](rust-2018/control-flow/async-await-for-easier-concurrency.md)
- [Trait 系统](rust-2018/trait-system/index.md)
- [`impl Trait` 轻松返回复杂的类型](rust-2018/trait-system/impl-trait-for-returning-complex-types-with-ease.md)
- [`dyn Trait` trait 对象](rust-2018/trait-system/dyn-trait-for-trait-objects.md)
- [支持 trait 对象的更多容器类型](rust-2018/trait-system/more-container-types-support-trait-objects.md)
- [相关常数](rust-2018/trait-system/associated-constants.md)
- [切片模式](rust-2018/slice-patterns.md)
- [Ownership and lifetimes](rust-2018/ownership-and-lifetimes/index.md)
- [Default `match` bindings](rust-2018/ownership-and-lifetimes/default-match-bindings.md)
- [`'_`, the anonymous lifetime](rust-2018/ownership-and-lifetimes/the-anonymous-lifetime.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# async/await for easier concurrency
# async/await 早期的并发

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

The initial release of Rust 2018 won't ship with `async`/`await` support, but
we have reserved the keywords so that a future release will contain them.
We'll update this page when it's closer to shipping!
Rust 2018 的初始版本不会附带 `async`/`await` 支持,但是我们保留了关键字,以便将来的版本包含它们。我们将在接近发布的时候更新此页面!
5 changes: 2 additions & 3 deletions src/rust-2018/control-flow/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Control flow
# 流程控制

[async_await]: async-await-for-easier-concurrency.md

In this chapter of the guide, we discuss a few improvements to control flow.
The most notable of these *will* be [`async` and `await`][async_await].
在本章节中,我们将讨论流程控制的改进,更多的关注点在 [`async``await`][async_await]
13 changes: 5 additions & 8 deletions src/rust-2018/control-flow/loops-can-break-with-a-value.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `loop`s can break with a value
# `loop` 可以 break 并携带返回值

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

`loop`s can now break with a value:
`loop` 可以 break 并携带返回值

```rust
// old code
Expand All @@ -17,10 +17,7 @@ loop {
let x = loop { break 7; };
```

Rust has traditionally positioned itself as an “expression oriented
language”, that is, most things are expressions that evaluate to a value,
rather than statements. `loop` stuck out as strange in this way, as it was
previously a statement.
Rust 传统上将自己定位为“面向表达式的语言”,也就是说,大多数事物都是评估价值而不是陈述表达。
`loop` 以这种方式突然变得奇怪,因为它之前是一个声明。

For now, this only applies to `loop`, and not things like `while` or `for`.
It's not clear yet, but we may add this to those in the future.
现在,这只适用于 `loop`,而不适用于 `while``for`。 目前尚不清楚,但我们可能会将此添加到未来。
41 changes: 16 additions & 25 deletions src/rust-2018/slice-patterns.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Slice patterns
# 切片模式

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

Have you ever tried to pattern match on the contents and structure of a slice?
Rust 2018 will let you do just that.
你有没有试过,用模式匹配去匹配切片的内容和结构? Rust 2018 将让你做到这一点。

For example, say we want to accept a list of names and respond to that with a
greeting. With slice patterns, we can do that easy as pie with:
例如,我们想要接受一个名单列表并回复问候语。使用切片模式,我们可以用以下方式轻松完成:

```rust
fn main() {
Expand All @@ -33,9 +31,9 @@ fn greet(people: &[&str]) {
}
```

Now, you don't have to check the length first.
现在,你不必检查长度了。

We can also match on arrays like so:
你也可以匹配 array 如下:

```rust
let arr = [1, 2, 3];
Expand All @@ -46,46 +44,39 @@ assert_eq!("ends with 3", match arr {
});
```

## More details
## 更多的细节

### Exhaustive patterns

In the first example, note in particular the `_ => ...` pattern.
Since we are matching on a slice, it could be of any length, so we need a
*"catch all pattern"* to handle it. If we forgot the `_ => ...` or
`identifier => ...` pattern, we would instead get an error saying:
### 穷举模式
在第一个例子中,注意匹配的 `_ => ...`。 如果开始匹配,那么将会匹配一切情况,所以有“穷尽所有模式”的处理方式。
如果我们忘记使用 `_ => ...` 或者 `identifier => ...` 模式,我们会得到如下的错误提醒:

```ignore
error[E0004]: non-exhaustive patterns: `&[_, _, _]` not covered
```

If we added a case for a slice of size `3` we would instead get:
如果我们再增加一项,我们将得到如下:

```ignore
error[E0004]: non-exhaustive patterns: `&[_, _, _, _]` not covered
```

and so on...

### Arrays and exact lengths
如此。

In the second example above, since arrays in Rust are of known lengths,
we have to match on exactly three elements.
If we try to match on 2 or 4 elements,we get the errors:
### 数组和精确的长度
在第二个例子中,数组是有固定长度的,我们需要匹配所有长度项,如果只匹配2,4项的话,会报错:

```ignore
error[E0527]: pattern requires 2 elements but array has 3
```

and

```ignore
error[E0527]: pattern requires 4 elements but array has 3
```

### In the pipeline
### 管道中

[the tracking issue]: https://github.com/rust-lang/rust/issues/23121

When it comes to slice patterns, more advanced forms are planned but
have not been stabilized yet. To learn more, follow [the tracking issue].
在切片模式方面,计划采用更先进的形式,但尚未稳定。要了解更多信息,请跟踪 [the tracking issue]
35 changes: 13 additions & 22 deletions src/rust-2018/trait-system/associated-constants.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Associated constants
# 相关常数

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

You can define traits, structs, and enums that have “associated functions”:
你可以定义具有“关联函数”的 traits, structs, enums

```rust
struct Struct;
Expand All @@ -18,11 +18,9 @@ fn main() {
}
```

These are called “associated functions” because they are functions that are
associated with the type, that is, they’re attached to the type itself, and
not any particular instance.
这个叫做“关联函数”,因为它关联了相关的类型,也就是说,它们附加到类型本身,而不是任何特定的实例。

Rust 1.20 adds the ability to define “associated constants” as well:
Rust 1.20 中为关联函数增加了新的功能:

```rust
struct Struct;
Expand All @@ -36,13 +34,10 @@ fn main() {
}
```

That is, the constant `ID` is associated with `Struct`. Like functions,
associated constants work with traits and enums as well.
其中,常量 `ID` 关联到 `Struct` 上,如果函数一样,关联常量也可以工作在 trait 和 enum 上。

Traits have an extra ability with associated constants that gives them some
extra power. With a trait, you can use an associated constant in the same way
you’d use an associated type: by declaring it, but not giving it a value. The
implementor of the trait then declares its value upon implementation:
Trait 具有额外的能力和相关的常数,为他们提供额外的力量。使用特征,您可以像使用关联类型一样
使用关联常量: 通过声明它,但不给它一个值。然后,特征的实现者在实现时声明其值:

```rust
trait Trait {
Expand All @@ -60,8 +55,7 @@ fn main() {
}
```

Before this feature, if you wanted to make a trait that represented floating
point numbers, you’d have to write this:
在此功能之前,如果要创建表示浮点数的特征,则必须如下:

```rust
trait Float {
Expand All @@ -71,10 +65,8 @@ trait Float {
}
```

This is slightly unwieldy, but more importantly, because they’re functions,
they cannot be used in constant expressions, even though they only return a
constant. Because of this, a design for `Float` would also have to include
constants as well:
这有点笨拙,但更重要的是,因为它们是函数,所以它们不能用于常量表达式,即使它们只返回常量。
因此,`Float` 的设计也必须包含常量:

```rust,ignore
mod f32 {
Expand All @@ -92,8 +84,7 @@ mod f32 {
}
```

Associated constants let you do this in a much cleaner way. This trait
definition:
关联常量让你可以更清晰的表达,如下:

```rust
trait Float {
Expand All @@ -103,7 +94,7 @@ trait Float {
}
```

Leads to this implementation:
继续实现如下:

```rust,ignore
mod f32 {
Expand All @@ -114,4 +105,4 @@ mod f32 {
}
```

much cleaner, and more versatile.
更加清晰,更通用。
25 changes: 10 additions & 15 deletions src/rust-2018/trait-system/dyn-trait-for-trait-objects.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# `dyn Trait` for trait objects
# `dyn Trait` trait 对象

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

The `dyn Trait` feature is the new syntax for using trait objects. In short:
`dyn Trait` 是使用 trait 对象的新语法,简而言之:

* `Box<Trait>` becomes `Box<dyn Trait>`
* `&Trait` and `&mut Trait` become `&dyn Trait` and `&mut dyn Trait`

And so on. In code:
因此,代码中:

```rust
trait Trait {}
Expand All @@ -25,18 +25,13 @@ fn function2() -> Box<dyn Trait> {
}
```

That's it!
这就是了!

## More details
## 更多细节
仅仅使用特征对象的特征名其实是个糟糕的决定。目前的语法通常含糊不清,即使对于老一批人来来说也是如此,
而且竟然没有它的替代品使用的更频繁,有时速度较慢,而且当其替代品可以使用时,它将根本不会被使用。

Using just the trait name for trait objects turned out to be a bad decision.
The current syntax is often ambiguous and confusing, even to veterans,
and favors a feature that is not more frequently used than its alternatives,
is sometimes slower, and often cannot be used at all when its alternatives can.
此外,随着 `impl Trait` 的到来,`impl Trait` vs `dyn Trait``impl Trait` vs `Trait` 更好更对称。
`impl Trait`将在下一节进一步解释。

Furthermore, with `impl Trait` arriving, "`impl Trait` vs `dyn Trait`" is much
more symmetric, and therefore a bit nicer, than "`impl Trait` vs `Trait`".
`impl Trait` is explained further in the next section.

In the new edition, you should therefore prefer `dyn Trait` to just `Trait`
where you need a trait object.
因此,在新版本中,选择使用 trait 对象时,你应该选 `dyn Trait` 而不是 `Trait`

0 comments on commit 534a54d

Please sign in to comment.