Skip to content

Commit

Permalink
1.16.0-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiserY committed Mar 17, 2017
1 parent c438bda commit 186bf54
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Rust 程序设计语言(第二版)翻译阅读:[https://kaisery.github.io/
[rustbook.cn](http://rustbook.cn/)[掘金](http://gold.xitu.io/)赞助

## Rust 版本
1.15.0-stable
1.16.0-stable

## 国内镜像
请参考[中文 Wiki](https://wiki.rust-china.org/%E5%9B%BD%E5%86%85%E9%95%9C%E5%83%8F)上的说明
Expand Down
2 changes: 1 addition & 1 deletion content/Box Syntax and Patterns 装箱语法和模式.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [box-syntax-and-patterns.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/box-syntax-and-patterns.md)
> <br>
> commit 024aa9a345e92aa1926517c4d9b16bd83e74c10d
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
目前唯一稳定的创建`Box`的方法是通过`Box::new`方法。并且不可能在一个模式匹配中稳定的析构一个`Box`。不稳定的`box`关键字可以用来创建和析构`Box`。下面是一个用例:

Expand Down
2 changes: 1 addition & 1 deletion content/Casting Between Types 类型转换.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [casting-between-types.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/casting-between-types.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit 40b9352aa371d5984c7e84cad5e2f597dbecf177
Rust,和它对安全的关注,提供了两种不同的在不同类型间转换的方式。第一个,`as`,用于安全转换。相反,`transmute`允许任意的转换,而这是 Rust 中最危险的功能之一!

Expand Down
2 changes: 1 addition & 1 deletion content/Closures 闭包.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [closures.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/closures.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit 57b53fdd9ef34bf4015b4fd7a202dfa485773c96
有时为了整洁和复用打包一个函数和**自由变量***free variables*)是很有用的。自由变量是指被用在函数中来自函数内部作用域并只用于函数内部的变量。对此,我们用一个新名字“闭包”而且 Rust 提供了大量关于他们的实现,正如我们将看到的。

Expand Down
2 changes: 1 addition & 1 deletion content/Crates and Modules crate 和模块.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [crates-and-modules.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/crates-and-modules.md)
> <br>
> commit 98c6770a231d9bbaede40bbcf659218995958d6a
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
当一个项目变大以后,良好的软件工程实践是把它分为一堆较小的部分,再把它们装配到一起。定义良好的接口也非常重要,以使有些功能是私有的,而有些是公有的。Rust 有一个模块系统来帮助我们处理这些工作。

Expand Down
4 changes: 2 additions & 2 deletions content/Documentation 文档.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [documentation.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/documentation.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit d8ee0745f734231edb16441776a60c33dae317c2
在任何软件项目中,文档都是重要的部分,其同样在Rust中是头等重要的。让我们讨论下Rust提供给我们的编写项目文档的工具。

Expand Down Expand Up @@ -387,7 +387,7 @@ $ cargo test
# fn foo() {}
~~~
`no_run`属性会编译你的代码,但是不运行它。这对像如“如何开始一个网络服务”这样的例子很重要,你会希望确保它能够编译,不过它可能会无限循环的执行!
`no_run`属性会编译你的代码,但是不运行它。这对像如“如何开始一个网络服务”这样的例子很重要,你会希望确保它能够编译,不过它可能会在一个没有网络连接的测试环境中运行。
### 文档化模块
Rust有另一种文档注释,`//!`。这种注释并不文档化接下来的内容,而是包围它的内容。换句话说:
Expand Down
26 changes: 25 additions & 1 deletion content/Foreign Function Interface 外部函数接口.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [ffi.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/ffi.md)
> <br>
> commit 614b74c24bb80e17230e58a74ef5a4725972f84a
> commit 5cdf128410e465fd5c0338daa48366ea5200bc32
## 介绍

Expand Down Expand Up @@ -461,6 +461,30 @@ Rust拥有的装箱(`Box<T>`)使用非空指针作为指向他包含的对

标准库中的`libc`模块包含类型别名和 C 标准库中的函数定义,Rust 默认链接`libc``libm`

## 可变参数函数

在 C 语言中,函数是“可变参数的”,这意味着它可以接受可变数量的参数。在 Rust 中可以通过在外部函数声明中指定`...`来实现:

```rust
extern {
fn foo(x: i32, ...);
}

fn main() {
unsafe {
foo(10, 20, 30, 40, 50);
}
}
```

普通的 Rust 不能是可变参数的:

```rust
// This will not compile

fn foo(x: i32, ...) { }
```

## “可空指针优化”(The "nullable pointer optimization")

特定的 Rust 类型被定义为永远不能为`null`。这包括引用(`&T``&mut T`),装箱(`Box<T>`),和函数指针(`extern "abi" fn()`)。当调用 C 接口时,可以为`null`的指针被广泛使用,这好像会需要使用到一些混乱的`transmutes`和/或不安全代码来处理与 Rust 类型间的相互转换。不过,Rust 语言提供了一个变通方案。
Expand Down
13 changes: 5 additions & 8 deletions content/Lang items 语言项.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [lang-items.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/lang-items.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit 893f42a83466cf02b6fd6d3c82d5419cdad47474
> **注意**:语言项通常由 Rust 发行版的 crate 提供,并且它自身有一个不稳定的接口。建议使用官方发布的 crate 而不是定义自己的版本。
Expand All @@ -11,15 +11,12 @@
例如,`Box`指针需要两个语言项,一个用于分配,一个用于释放。下面是一个独立的程序使用`Box`语法糖进行动态分配,通过`malloc``free`

```rust,ignore
#![feature(lang_items, box_syntax, start, libc)]
#![feature(lang_items, box_syntax, start, libc, core_intrinsics)]
#![no_std]
use core::intrinsics;
extern crate libc;
extern {
fn abort() -> !;
}
#[lang = "owned_box"]
pub struct Box<T>(*mut T);
Expand All @@ -29,7 +26,7 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
// Check if `malloc` failed:
if p as usize == 0 {
abort();
intrinsics::abort();
}
p
Expand All @@ -53,7 +50,7 @@ fn main(argc: isize, argv: *const *const u8) -> isize {
}
#[lang = "eh_personality"] extern fn rust_eh_personality() {}
#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { loop {} }
#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { unsafe { intrinsics::abort() }
# #[lang = "eh_unwind_resume"] extern fn rust_eh_unwind_resume() {}
# #[no_mangle] pub extern fn rust_eh_register_frames () {}
# #[no_mangle] pub extern fn rust_eh_unregister_frames () {}
Expand Down
2 changes: 1 addition & 1 deletion content/Lifetimes 生命周期.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [lifetimes.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/lifetimes.md)
> <br>
> commit ce57c66f5ca6120345bfc0828ebddd1b2f1d615e
> commit ee2abe85a6ed9dacd03c4113445053b9bde1ccaf
这篇教程是现行 3 个 Rust 所有权系统章节的第三部分。所有权系统是 Rust 最独特且最引人入胜的特性之一,也是作为 Rust 开发者应该熟悉的。Rust 所追求最大的目标 -- 内存安全,关键在于所有权。所有权系统有一些不同的概念,每个概念独自成章:

Expand Down
4 changes: 3 additions & 1 deletion content/Loops 循环.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

> [loops.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/loops.md)
> <br>
> commit 3b5d71e0cfb2d81f588a0b8929e796f3b68488e0
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
Rust 目前提供 3 种方法来进行一些迭代操作。他们是`loop``while``for`。每种方法都有自己的用途。

## loop

无限`loop`是 Rust 提供的最简单的循环。使用`loop`关键字,Rust 提供了一个直到一些终止语句被执行的循环方法。Rust 的无限`loop`看起来像这样:

```rust
Expand All @@ -16,6 +17,7 @@ loop {
```

## while

Rust 也有一个`while`循环。它看起来像:

```rust
Expand Down
4 changes: 2 additions & 2 deletions content/Match 匹配.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [match.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/match.md)
> <br>
> commit ccafdae9a11925cbc79c6ea4446688ef71bae1a1
> commit e7d60c00c68e72fa954bcff311bc92e1c445823c
一个简单的[`if`](If If语句.md)/`else`往往是不够的,因为你可能有两个或更多个选项。这样`else`也会变得异常复杂。Rust 有一个`match`关键字,它可以让你有效的取代复杂的`if`/`else`组。看看下面的代码:

Expand Down Expand Up @@ -65,7 +65,7 @@ fn process_message(msg: Message) {
match msg {
Message::Quit => quit(),
Message::ChangeColor(r, g, b) => change_color(r, g, b),
Message::Move { x: x, y: y } => move_cursor(x, y),
Message::Move { x, y: new_name_for_y } => move_cursor(x, new_name_for_y),
Message::Write(s) => println!("{}", s),
};
}
Expand Down
12 changes: 7 additions & 5 deletions content/No stdlib 不使用标准库.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [no-stdlib.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/no-stdlib.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit 893f42a83466cf02b6fd6d3c82d5419cdad47474
Rust 的标准库提供了很多有用的功能,不过它假设它的 host 系统的多种功能的支持:线程,网络,堆分配和其他功能。有些系统并没有这些功能,不过,Rust也能在这些系统上工作。为此,我们可以通过一个属性来告诉 Rust 我们不想使用标准库:`#![no_std]`

Expand All @@ -28,9 +28,10 @@ libc = { version = "0.2.14", default-features = false }
被标记为`#[start]`的函数传递的参数格式与 C 一致:

```rust
#![feature(lang_items)]
#![feature(lang_items, core_intrinsics)]
#![feature(start)]
#![no_std]
use core::intrinsics;

// Pull in the system libc library for what crt0.o likely requires.
extern crate libc;
Expand Down Expand Up @@ -60,17 +61,18 @@ pub extern fn rust_eh_unwind_resume() {
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
_file: &'static str,
_line: u32) -> ! {
loop {}
unsafe { intrinsics::abort() }
}
```

要 override 编译器插入的`main` shim,你必须使用`#![no_main]`禁用它并通过正确的 ABI 和正确的名字来创建合适的函数,这也需要需要覆盖编译器的命名改编:

```rust
#![feature(lang_items)]
#![feature(lang_items, core_intrinsics)]
#![feature(start)]
#![no_std]
#![no_main]
use core::intrinsics;

// Pull in the system libc library for what crt0.o likely requires.
extern crate libc;
Expand Down Expand Up @@ -100,7 +102,7 @@ pub extern fn rust_eh_unwind_resume() {
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
_file: &'static str,
_line: u32) -> ! {
loop {}
unsafe { intrinsics::abort() }
}
```

Expand Down
31 changes: 29 additions & 2 deletions content/Patterns 模式.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [patterns.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/patterns.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit 0dad9dcf9ec7284283ef97dd0f192060a70cfad5
模式在Rust中十分常见。我们在[变量绑定](Variable Bindings 变量绑定.md),[匹配表达式](Match 匹配.md)和其它一些地方使用它们。让我们开始一个快速的关于模式可以干什么的教程!

Expand All @@ -21,7 +21,34 @@ match x {

这会打印出`one`

有一个模式的陷阱:就像任何引入一个新绑定的语句,他们会引入隐藏。例如:
可以在任何分支创建值的绑定:

```rust
let x = 1;

match x {
y => println!("x: {} y: {}", x, y),
}
```

这会打印出:

```text
x: 1 y: 1
```

注意在同一匹配块中同时拥有捕获全部的`_`和捕获全部的绑定会产生错误:

```rust
let x = 1;

match x {
y => println!("x: {} y: {}", x, y),
_ => println!("anything"), // this causes an error as it is unreachable
}
```

这里有一个模式的陷阱:就像任何引入一个新绑定的语句,他们会引入隐藏。例如:

```rust
let x = 1;
Expand Down
2 changes: 1 addition & 1 deletion content/Procedural Macros 过程宏.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [procedural-macros.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/procedural-macros.md)
> <br>
> commit 2cb7a60ef6bdc38c8ca9dcf26c170c46d67e1b1d
> commit 3075c1f65e08e0b52dcf872588358daffef8b47c
在本书接下来的部分,你将看到 Rust 提供了一个叫做“导出(derive)”的机制来轻松的实现 trait。例如,

Expand Down
6 changes: 3 additions & 3 deletions content/Strings 字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [strings.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/strings.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit d83fff3b3b9dd0fd6eef862e97f883d171367041
对于每一个程序,字符串都是需要掌握的重要内容。由于Rust主要着眼于系统编程,所以它的字符串处理系统与其它语言有些许区别。每当你碰到一个可变大小的数据结构时,情况都会变得很微妙,而字符串正是可变大小的数据结构。这也就是说,Rust的字符串与一些像C这样的系统编程语言也不相同。

Expand Down Expand Up @@ -137,8 +137,8 @@ let hachi = &dog[0..2];
给出如下错误:

```text
thread 'main' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
character boundary'
thread 'main' panicked at 'byte index 2 is not a char boundary; it is inside '忠'
(bytes 0..3) of `忠犬ハチ公`'
```

## 连接(Concatenation)
Expand Down
4 changes: 2 additions & 2 deletions content/Syntax Index 语法索引.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [syntax-index.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/syntax-index.md)
> <br>
> commit 1e9aad752beb789df4f2232adf6b9a24efbdac8d
> commit 4093bafe636bb711228e76d780d520626df28921
## 关键词(Keywords)

Expand Down Expand Up @@ -49,7 +49,7 @@
* `%` (`expr % expr`): 算数取余。可重载(`Rem`)。
* `%=` (`var %= expr`): 算数取余并赋值。可重载(`RemAssign`)。
* `&` (`expr & expr`): 位计算和。可重载(`BitAnd`)。
* `&` (`&expr`): 借用。详见[引用和借用](References and Borrowing 引用和借用.md)。
* `&` (`&expr`, `&mut expr`): 借用。详见[引用和借用](References and Borrowing 引用和借用.md)。
* `&` (`&type`, `&mut type`, `&'a type`, `&'a mut type`): 借用指针类型。详见[引用和借用](References and Borrowing 引用和借用.md)。
* `&=` (`var &= expr`): 位计算和并赋值。可重载(`BitAndAssign`)。
* `&&` (`expr && expr`): 逻辑和。
Expand Down
4 changes: 2 additions & 2 deletions content/Testing 测试.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [testing.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/testing.md)
> <br>
> commit d950ca175ab5e7a9827353f9fb7d9d6e3f6e7658
> commit c4c86dd04ccf6e9a6ac9282ecb9bb42e13ea5dad
> Program testing can be a very effective way to show the presence of bugs, but it is hopelessly inadequate for showing their absence.
Expand Down Expand Up @@ -538,7 +538,7 @@ test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured

## 测试与并发

在编写测试时需要注意的一个重要的情况是它们可能使用线程来并发的运行。为此需要注意要以一种测试之间不会相互依赖的方式编写,亦不能有任何共享的状态。“共享状态”可以包括运行环境,例如当前工作目录(cwd),或者环境变量。
特别需要注意的是测试使用线程来并发的运行。为此需要注意测试之间不能相互依赖也不能依赖任何共享状态。“共享状态”可以包括运行环境,例如当前工作目录(cwd),或者环境变量。

如果这样做有问题控制这些并发也是可能的,要么设置环境变量`RUST_TEST_THREADS`,或者向测试传递`--test-threads`用来比较两个参数:

Expand Down
4 changes: 2 additions & 2 deletions content/Trait Objects trait 对象.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [trait-objects.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/trait-objects.md)
> <br>
> commit 28548db57d0acbc00ee80b43816953dbe31d53ba
> commit c7b092b47d8e50b58c975040af0a84a544f7fa7a
当涉及到多态的代码时,我们需要一个机制来决定哪个具体的版本应该得到执行。这叫做“分发”(dispatch)。大体上有两种形式的分发:静态分发和动态分发。虽然 Rust 喜欢静态分发,不过它也提供了一个叫做“trait 对象”的机制来支持动态分发。

Expand Down Expand Up @@ -203,7 +203,7 @@ static Foo_for_String_vtable: FooVtable = FooVtable {
};
```

在每个虚表中的`destructor`字段指向一个会清理虚表类型的任何资源的函数,对于`u8`是普通的,不过对于`String`它会释放内存。这对于像`Box<Foo>`这类有所有权的trait对象来说是必要的,它需要在离开作用域后清理`Box`以及它内部的类型所分配的。`size``align`字段储存需要清除类型的大小和它的对齐情况;它们原理上是无用的因为这些信息已经嵌入了析构函数中,不过在将来会被使用到,因为 trait 对象正日益变得更灵活
在每个虚表中的`destructor`字段指向一个会清理虚表类型的任何资源的函数,对于`u8`是普通的,不过对于`String`它会释放内存。这对于像`Box<Foo>`这类有所有权的trait对象来说是必要的,它需要在离开作用域后清理`Box`以及它内部的类型所分配的。`size``align`字段储存需要清除类型的大小和它的对齐需求

假设我们有一些实现了`Foo`的值,那么显式的创建和使用`Foo`trait对象可能看起来有点像这个(忽略不匹配的类型,它们只是指针而已):

Expand Down
2 changes: 1 addition & 1 deletion content/Variable Bindings 变量绑定.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [variable-bindings.md](https://github.com/rust-lang/rust/blob/stable/src/doc/book/variable-bindings.md)
> <br>
> commit 49c6b3c23f686448d1ac888739d76b11cbe6355e
> commit 20abf050e7f5698cd1012de00295ec805143735a
事实上每一个非“Hello World” Rust 程序都用了**变量绑定**。他们将一些值绑定到一个名字上,这样可以在之后使用他们。`let`被用来声明一个绑定,像这样:

Expand Down
2 changes: 1 addition & 1 deletion preface.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Rust 程序设计语言(第二版)翻译阅读:[https://kaisery.github.io/
[rustbook.cn](http://rustbook.cn/)[掘金](http://gold.xitu.io/)赞助

## Rust 版本
1.15.0-stable
1.16.0-stable

## 相关资源
* GitHub: https://github.com/KaiserY/rust-book-chinese
Expand Down

0 comments on commit 186bf54

Please sign in to comment.