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

Commit

Permalink
translate 3.6 & 3.7 & 3.8 & 3.9 & 3.10 & 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Aug 22, 2018
1 parent 534a54d commit 02c835a
Show file tree
Hide file tree
Showing 24 changed files with 272 additions and 430 deletions.
90 changes: 45 additions & 45 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,52 @@
- [支持 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)
- [Lifetime elision in `impl`](rust-2018/ownership-and-lifetimes/lifetime-elision-in-impl.md)
- [`T: 'a` inference in structs](rust-2018/ownership-and-lifetimes/inference-in-structs.md)
- [Simpler lifetimes in `static` and `const`](rust-2018/ownership-and-lifetimes/simpler-lifetimes-in-static-and-const.md)
- [Data types](rust-2018/data-types/index.md)
- [Field init shorthand](rust-2018/data-types/field-init-shorthand.md)
- [`..=` for inclusive ranges](rust-2018/data-types/inclusive-ranges.md)
- [128 bit integers](rust-2018/data-types/128-bit-integers.md)
- ["Operator-equals" are now implementable](rust-2018/data-types/operator-equals-are-now-implementable.md)
- [`union` for an unsafe form of `enum`](rust-2018/data-types/union-for-an-unsafe-form-of-enum.md)
- [Choosing alignment with the `repr` attribute](rust-2018/data-types/choosing-alignment-with-the-repr-attribute.md)
- [SIMD for faster computing](rust-2018/simd-for-faster-computing.md)
- [Macros](rust-2018/macros/index.md)
- [Custom Derive](rust-2018/macros/custom-derive.md)
- [Macro changes](rust-2018/macros/macro-changes.md)
- [The compiler](rust-2018/the-compiler/index.md)
- [Improved error messages](rust-2018/the-compiler/improved-error-messages.md)
- [Incremental Compilation for faster compiles](rust-2018/the-compiler/incremental-compilation-for-faster-compiles.md)
- [An attribute for deprecation](rust-2018/the-compiler/an-attribute-for-deprecation.md)
- [Rustup for managing Rust versions](rust-2018/rustup-for-managing-rust-versions.md)
- [Cargo and crates.io](rust-2018/cargo-and-crates-io/index.md)
- [`cargo check` for faster checking](rust-2018/cargo-and-crates-io/cargo-check-for-faster-checking.md)
- [`cargo install` for easy installation of tools](rust-2018/cargo-and-crates-io/cargo-install-for-easy-installation-of-tools.md)
- [`cargo new` defaults to a binary project](rust-2018/cargo-and-crates-io/cargo-new-defaults-to-a-binary-project.md)
- [`cargo rustc` for passing arbitrary flags to `rustc`](rust-2018/cargo-and-crates-io/cargo-rustc-for-passing-arbitrary-flags-to-rustc.md)
- [Cargo workspaces for multi-package projects](rust-2018/cargo-and-crates-io/cargo-workspaces-for-multi-package-projects.md)
- [Multi-file `examples`](rust-2018/cargo-and-crates-io/multi-file-examples.md)
- [Replacing dependencies with `patch`](rust-2018/cargo-and-crates-io/replacing-dependencies-with-patch.md)
- [Cargo can use a local registry replacement](rust-2018/cargo-and-crates-io/cargo-can-use-a-local-registry-replacement.md)
- [Crates.io disallows wildcard dependencies](rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.md)
- [Documentation](rust-2018/documentation/index.md)
- [New editions of the "the book"](rust-2018/documentation/new-editions-of-the-book.md)
- [所有权和生命周期](rust-2018/ownership-and-lifetimes/index.md)
- [默认 `match` 绑定](rust-2018/ownership-and-lifetimes/default-match-bindings.md)
- [`'_` 匿名生命周期](rust-2018/ownership-and-lifetimes/the-anonymous-lifetime.md)
- [`impl`中省略生命周期](rust-2018/ownership-and-lifetimes/lifetime-elision-in-impl.md)
- [`T: 'a` 结构体中的推导](rust-2018/ownership-and-lifetimes/inference-in-structs.md)
- [`static``const`中更简单的生命周期](rust-2018/ownership-and-lifetimes/simpler-lifetimes-in-static-and-const.md)
- [数据类型](rust-2018/data-types/index.md)
- [字段初始化简写](rust-2018/data-types/field-init-shorthand.md)
- [`..=` 包含取值范围](rust-2018/data-types/inclusive-ranges.md)
- [128位整型](rust-2018/data-types/128-bit-integers.md)
- ["Operator-equals" 的实现](rust-2018/data-types/operator-equals-are-now-implementable.md)
- [`union`: 一个非安全的 `enum`](rust-2018/data-types/union-for-an-unsafe-form-of-enum.md)
- [`repr` 属性的对齐方式](rust-2018/data-types/choosing-alignment-with-the-repr-attribute.md)
- [SIMD 更快的计算](rust-2018/simd-for-faster-computing.md)
- [](rust-2018/macros/index.md)
- [自定义 Derive](rust-2018/macros/custom-derive.md)
- [宏的变化](rust-2018/macros/macro-changes.md)
- [编译器](rust-2018/the-compiler/index.md)
- [改进错误信息](rust-2018/the-compiler/improved-error-messages.md)
- [增量编译以加快编译速度](rust-2018/the-compiler/incremental-compilation-for-faster-compiles.md)
- [弃用属性](rust-2018/the-compiler/an-attribute-for-deprecation.md)
- [RustupRust 版本管理器](rust-2018/rustup-for-managing-rust-versions.md)
- [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 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)
- [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)
- [The Rust Bookshelf](rust-2018/documentation/the-rust-bookshelf.md)
- [The Rustonomicon](rust-2018/documentation/the-rustonomicon.md)
- [Full documentation for `std::os`](rust-2018/documentation/std-os-has-documentation-for-all-platforms.md)
- [`std::os`的详尽文档](rust-2018/documentation/std-os-has-documentation-for-all-platforms.md)
- [`rustdoc`](rust-2018/rustdoc/index.md)
- [Documentation tests can now `compile-fail`](rust-2018/rustdoc/documentation-tests-can-now-compile-fail.md)
- [Rustdoc uses CommonMark](rust-2018/rustdoc/rustdoc-uses-commonmark.md)
- [Platform and target support](rust-2018/platform-and-target-support/index.md)
- [`libcore` for low-level Rust](rust-2018/platform-and-target-support/libcore-for-low-level-rust.md)
- [WebAssembly support](rust-2018/platform-and-target-support/webassembly-support.md)
- [Global allocators](rust-2018/platform-and-target-support/global-allocators.md)
- [MSVC toolchain support](rust-2018/platform-and-target-support/msvc-toolchain-support.md)
- [MUSL support for fully static binaries](rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.md)
- [`cdylib` crates for C interoperability](rust-2018/platform-and-target-support/cdylib-crates-for-c-interoperability.md)
- [文档测试 `compile-fail`](rust-2018/rustdoc/documentation-tests-can-now-compile-fail.md)
- [Rustdoc 使用 CommonMark](rust-2018/rustdoc/rustdoc-uses-commonmark.md)
- [平台支持和target](rust-2018/platform-and-target-support/index.md)
- [`libcore` 低层 Rust 使用](rust-2018/platform-and-target-support/libcore-for-low-level-rust.md)
- [WebAssembly 支持](rust-2018/platform-and-target-support/webassembly-support.md)
- [全局分配符](rust-2018/platform-and-target-support/global-allocators.md)
- [MSVC toolchain 支持](rust-2018/platform-and-target-support/msvc-toolchain-support.md)
- [MUSL 支持完全静态二进制文件](rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.md)
- [`cdylib` 与 C 的互通性](rust-2018/platform-and-target-support/cdylib-crates-for-c-interoperability.md)

- [Unstable feature status](unstable-feature-status.md)
- [不稳定的特性状态](unstable-feature-status.md)
7 changes: 3 additions & 4 deletions src/rust-2018/cargo-and-crates-io/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Cargo and crates.io
# Cargo crates.io

[check]: improved-error-messages.md
[check]: cargo-check-for-faster-checking.md

In this chapter of the guide, we discuss a few improvements to `cargo` and crates.io.
A notable addition here is the new [`cargo check`][check] command.
在本章中,我们将讨论关于 `cargo` 和 crates.io 的改进。更多的关注在 [`cargo check`][check]
8 changes: 4 additions & 4 deletions src/rust-2018/data-types/128-bit-integers.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# 128 bit integers
# 128位整型

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

A very simple feature: Rust now has 128 bit integers!
这是一个简单的特性: Rust 现在支持128位的整型了!

```rust
let x: i128 = 0;
let y: u128 = 0;
```

These are twice the size of `u64`, and so can hold more values. More specifically,
这将是 u64的两倍大小,并且可以存放更多的值,十分方便。

* `u128`: `0` - `340,282,366,920,938,463,463,374,607,431,768,211,455`
* `i128`: `−170,141,183,460,469,231,731,687,303,715,884,105,728` - `170,141,183,460,469,231,731,687,303,715,884,105,727`

Whew!
!
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# Choosing alignment with the repr attribute
# repr 属性的对齐方式

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

From [Wikipedia](https://en.wikipedia.org/wiki/Data_structure_alignment):
[Wikipedia](https://en.wikipedia.org/wiki/Data_structure_alignment):

> The CPU in modern computer hardware performs reads and writes to memory
> most efficiently when the data is naturally aligned, which generally means
> that the data address is a multiple of the data size. Data alignment refers
> to aligning elements according to their natural alignment. To ensure natural
> alignment, it may be necessary to insert some padding between structure
> elements or after the last element of a structure.
> 现代计算机硬件中的 CPU 在数据自然对齐时,可以最有效地执行对存储器的读写,这通常意味着数据地址是数据大小的倍数。
> 数据对齐是指根据元素的自然对齐来对齐元素。为了确保自然对齐,可能需要在结构元素之间或结构的最后一个元素之后插入一些填充值。
The `#[repr]` attribute has a new parameter, `align`, that sets the alignment of your struct:
`#[repr]` 属性有一个新参数 `align`,用于设置结构体的对齐方式:

```rust
struct Number(i32);
Expand All @@ -26,30 +22,13 @@ assert_eq!(std::mem::align_of::<Align16>(), 16);
assert_eq!(std::mem::size_of::<Align16>(), 16);
```

If you’re working with low-level stuff, control of these kinds of things can
be very important!

The alignment of a type is normally not worried about as the compiler will
"do the right thing" of picking an appropriate alignment for general use
cases. There are situations, however, where a nonstandard alignment may be
desired when operating with foreign systems. For example these sorts of
situations tend to necessitate or be much easier with a custom alignment:

* Hardware can often have obscure requirements such as "this structure is
aligned to 32 bytes" when it in fact is only composed of 4-byte values. While
this can typically be manually calculated and managed, it's often also useful
to express this as a property of a type to get the compiler to do a little
extra work instead.
* C compilers like `gcc` and `clang` offer the ability to specify a custom
alignment for structures, and Rust can much more easily interoperate with
these types if Rust can also mirror the request for a custom alignment (e.g.
passing a structure to C correctly is much easier).
* Custom alignment can often be used for various tricks here and there and is
often convenient as "let's play around with an implementation" tool. For
example this can be used to statically allocate page tables in a kernel or
create an at-least cache-line-sized structure easily for concurrent
programming.

The purpose of this feature is to provide a lightweight annotation to alter
the compiler-inferred alignment of a structure to enable these situations
much more easily.
如果您正在使用底层级别的东西,控制这些事情可能非常重要!

一般来说,类型的对齐并不担心,因为编译器将为一般用例选择适当的对齐“做正确的事情”。
但是,在使用外部系统操作时,可能需要非标准对齐。例如,通过自定义对齐,这些情况往往需要或更容易:

* 当硬件实际上仅由4字节值组成时,硬件通常具有模糊的要求,例如“此结构对齐到32字节”。虽然这通常可以手动计算和管理,但将它表达为类型的属性通常也很有用,可以让编译器做一些额外的工作。
*`gcc``clang` 这样的C编译器提供了为结构指定自定义对齐的能力,如果 Rust 也可以镜像自定义对齐的请求(例如将结构传递给C),Rust 可以更容易地与这些类型进行互操作。正确的更容易)。
* 自定义对齐通常可以在这里和那里用于各种技巧,并且通常很方便,因为“让我们使用实现”工具。例如,这可用于在内核中静态分配页表,或者为并发编程轻松创建至少缓存行大小的结构。

此功能的目的是提供轻量级注释,以更改结构的编译器推断对齐,从而更轻松地启用这些情况。
12 changes: 5 additions & 7 deletions src/rust-2018/data-types/field-init-shorthand.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Field init shorthand
# 字段初始化简写

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

In older Rust, when initializing a struct, you must always give the full set of `key: value` pairs
for its fields:
在以往的 Rust 中,当初始化一个结构体的时候,总是需要完全按照 `key: value` 对的写法:

```rust
struct Point {
Expand All @@ -20,8 +19,7 @@ let p = Point {
};
```

However, often these variables would have the same names as the fields. So you'd end up
with code that looks like this:
但是,这些字段通常会是相同的名字,所以你可以把它写成这样:

```rust,ignore
let p = Point {
Expand All @@ -30,7 +28,7 @@ let p = Point {
};
```

Now, if the variable is of the same name, you don't have to write out both, just write out the key:
现在,如果变量名和结构体字段名相同,可以省略写成这样:

```rust
struct Point {
Expand All @@ -46,4 +44,4 @@ let p = Point {
x,
y,
};
```
```
24 changes: 9 additions & 15 deletions src/rust-2018/data-types/inclusive-ranges.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
# `..=` for inclusive ranges
# `..=` 包含取值范围

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

Since well before Rust 1.0, you’ve been able to create exclusive ranges with
.. like this:
在以前的 Rust 1.0 中,我们像下面这样写一个取值范围:

```
for i in 1..3 {
println!("i: {}", i);
}
```

This will print `i: 1` and then `i: 2`. Today, you can now create an
inclusive range, like this:
这将会打印 `i: 1` 然后是 `i: 2`,现在你可以这么写:

```rust
for i in 1..=3 {
println!("i: {}", i);
}
```

This will print `i: 1` and then `i: 2` like before, but also `i: 3`; the
three is included in the range. Inclusive ranges are especially useful if you
want to iterate over every possible value in a range. For example, this is a
surprising Rust program:
这也会打印 `i: 1` 然后是 `i: 2`,最后是 `i: 3`; 最后的也将会包含在范围取值中,
当你需要包含取值范围的时候,这会十分有用。下面是一个令人惊奇的例子:

```rust
fn takes_u8(x: u8) {
Expand All @@ -38,8 +34,7 @@ fn main() {
}
```

What does this program do? The answer: nothing. The warning we get when
compiling has a hint:
这个程序做了什么?回答是:没有任何东西。编译器将会报出警告:

```text
warning: literal out of range for u8
Expand All @@ -51,10 +46,9 @@ warning: literal out of range for u8
= note: #[warn(overflowing_literals)] on by default
```

That’s right, since `i` is a `u8`, this overflows, and is the same as writing
`for i in 0..0`, so the loop executes zero times.
这是正常的,因为 `i` 作为一个 `u8`,256超出了范围,这就导致效果和 `for i in 0..0` 一样, 这段代码将执行0次。

We can do this with inclusive ranges, however:
但是,我们现在可以这么写:

```rust
fn takes_u8(x: u8) {
Expand All @@ -69,4 +63,4 @@ fn main() {
}
```

This will produce those 256 lines of output you might have been expecting.
这个将会执行256次你需要执行的内容。
5 changes: 2 additions & 3 deletions src/rust-2018/data-types/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Data types
# 数据类型

[fis]: field-init-shorthand.md

In this chapter of the guide, we discuss a few improvements to data types.
One of these are [field-init-shorthand][fis].
在本章节中,我们讨论数据类型的改进,值得关注的是 [字段初始化简写][fis]
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# "Operator-equals" are now implementable
# "Operator-equals" 现在实现了

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

The various “operator equals” operators, such as `+=` and `-=`, are
implementable via various traits. For example, to implement `+=` on
a type of your own:
各种各样的 “等价操作符” 已经被各种各样的trait实现了, 比如 `+=``-=`
举个例子:下面是一个 `+=` 操作符:

```rust
use std::ops::AddAssign;
Expand All @@ -30,4 +29,4 @@ fn main() {
}
```

This will print `Count { value: 6 }`.
这将打印 `Count { value: 6 }`.

0 comments on commit 02c835a

Please sign in to comment.