mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-02-23 04:32:16 +08:00
Merge pull request #223 from Turing-Chu/remote
appendix-06 and some update
This commit is contained in:
commit
6ba444e2ee
@ -98,7 +98,7 @@
|
|||||||
- [共享状态](ch16-03-shared-state.md)
|
- [共享状态](ch16-03-shared-state.md)
|
||||||
- [可扩展的并发:`Sync` 与 `Send`](ch16-04-extensible-concurrency-sync-and-send.md)
|
- [可扩展的并发:`Sync` 与 `Send`](ch16-04-extensible-concurrency-sync-and-send.md)
|
||||||
|
|
||||||
- [Rust 的面向对象编程特征](ch17-00-oop.md)
|
- [Rust 的面向对象编程特性](ch17-00-oop.md)
|
||||||
- [面向对象语言的特点](ch17-01-what-is-oo.md)
|
- [面向对象语言的特点](ch17-01-what-is-oo.md)
|
||||||
- [为使用不同类型的值而设计的 trait 对象](ch17-02-trait-objects.md)
|
- [为使用不同类型的值而设计的 trait 对象](ch17-02-trait-objects.md)
|
||||||
- [面向对象设计模式的实现](ch17-03-oo-design-patterns.md)
|
- [面向对象设计模式的实现](ch17-03-oo-design-patterns.md)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# 附录C - 可派生的 trait
|
## 附录C - 可派生的 trait
|
||||||
|
|
||||||
> [appendix-03-derivable-traits.md][appendix-03]
|
> [appendix-03-derivable-traits.md][appendix-03]
|
||||||
> <br />
|
> <br />
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
## 附录E: 本书翻译
|
## 附录E - 本书翻译
|
||||||
|
|
||||||
> [appendix-05-translation.md](https://github.com/rust-lang/book/blob/master/second-edition/src/appendix-05-translation.md)
|
> [appendix-05-translation.md](https://github.com/rust-lang/book/blob/master/second-edition/src/appendix-05-translation.md)
|
||||||
> <br />
|
> <br />
|
||||||
|
@ -1 +1,113 @@
|
|||||||
## F - 最新功能
|
## F - 最新功能
|
||||||
|
|
||||||
|
> [appendix-06-newest-features.md](https://github.com/rust-lang/book/blob/master/second-edition/src/appendix-06-newest-features.md)
|
||||||
|
> <br />
|
||||||
|
> commit b64de01431cdf1020ad3358d2f83e46af68a39ed
|
||||||
|
|
||||||
|
自从本书的主要部分完成之后,该附录文档中的特性已经加到 Rust 的稳定版中。
|
||||||
|
|
||||||
|
### 字段初始化缩写
|
||||||
|
|
||||||
|
我们可以通过具名字段来初始化一个数据结构(结构体、枚举、联合体),如将 `fieldname: fieldname` 缩写为 `fieldname` 。这可以以更少的重复代码来完成一个复杂的初始化语法。
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Person {
|
||||||
|
name: String,
|
||||||
|
age: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let name = String::from("Peter");
|
||||||
|
let age = 27;
|
||||||
|
|
||||||
|
// Using full syntax:
|
||||||
|
let peter = Person { name: name, age: age };
|
||||||
|
|
||||||
|
let name = String::from("Portia");
|
||||||
|
let age = 27;
|
||||||
|
|
||||||
|
// Using field init shorthand:
|
||||||
|
let portia = Person { name, age };
|
||||||
|
|
||||||
|
println!("{:?}", portia);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 从循环中返回( loop )
|
||||||
|
|
||||||
|
`loop` 的用法之一是重试一个可以操作,比如检查线程是否完成其任务。然而可能需要将该操作的结果传到其他部分代码。如果加上 `break` 表达式来停止循环,则会从循环中断中返回:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
let result = loop {
|
||||||
|
counter += 1;
|
||||||
|
|
||||||
|
if counter == 10 {
|
||||||
|
break counter * 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(result, 20);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## `use` 声明中的内置组
|
||||||
|
|
||||||
|
如果有一个包含许多不同子模块的复杂模块树,然后需要从每个子模块中引入几个特性,那么将所有导入模块放在同一声明中来保持代码清洁同时避免根模块名重复将会非常有用。
|
||||||
|
|
||||||
|
`use` 声明所支持的嵌套在这些情况下对你有帮助:简单的导入和全局导入。例如,下面的代码片段导入了 `bar` 、 `Foo` 、 `baz` 中所有项和 `Bar` :
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# #![allow(unused_imports, dead_code)]
|
||||||
|
#
|
||||||
|
# mod foo {
|
||||||
|
# pub mod bar {
|
||||||
|
# pub type Foo = ();
|
||||||
|
# }
|
||||||
|
# pub mod baz {
|
||||||
|
# pub mod quux {
|
||||||
|
# pub type Bar = ();
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
use foo::{
|
||||||
|
bar::{self, Foo},
|
||||||
|
baz::{*, quux::Bar},
|
||||||
|
};
|
||||||
|
#
|
||||||
|
# fn main() {}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 范围包含
|
||||||
|
|
||||||
|
先前,当在表达式中使用范围( `..` 或 `...` )时,其必须使用排除上界的 `..` ,而在模式中,则要使用包含上界的 `...` 。而现在,`..=` 可作为语法用于表达式或范围上下文件中的包含范围中。
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
for i in 0 ..= 10 {
|
||||||
|
match i {
|
||||||
|
0 ..= 5 => println!("{}: low", i),
|
||||||
|
6 ..= 10 => println!("{}: high", i),
|
||||||
|
_ => println!("{}: out of range", i),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`...` 语法也可用在匹配语法中,但不能用于表达式中,而 `..=` 则二者皆可。
|
||||||
|
|
||||||
|
## 128 字节的整数
|
||||||
|
|
||||||
|
Rust 1.26.0 添加了 128 字节的整数基本类型:
|
||||||
|
|
||||||
|
- `u128`: 一个在 [0, 2^128 - 1] 范围内的 128 字节的无符号整数
|
||||||
|
- `i128`: 一个在 [-(2^127), 2^127 - 1] 范围内的有符号整数
|
||||||
|
|
||||||
|
这俩基本类型由 LLVM 支持高效地实现。即使在不支持 128 字节整数的平台上,它们都是可用的,且可像其他整数类型那样使用。
|
||||||
|
|
||||||
|
这俩基本类型在那些需要高效使用大整数的算法中非常有用,如某些加密算法。
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
# Rust 是一个面向对象的编程语言吗?
|
# Rust 的面向对象特性
|
||||||
|
|
||||||
> [ch17-00-oop.md](https://github.com/rust-lang/book/blob/master/second-edition/src/ch17-00-oop.md)
|
> [ch17-00-oop.md][ch17-00]
|
||||||
> <br>
|
> <br>
|
||||||
> commit 28d0efb644d18e8d104c2e813c8cdce50d040d3d
|
> commit 07b0ca8c829af09d60ab4eb9e69584b6f4a96f60
|
||||||
|
|
||||||
面向对象编程(Object-Oriented Programming)是一种起源于 20 世纪 60 年代的 Simula 编程语言的模式化编程方式,然后在 90 年代随着 C++ 语言开始流行。关于 OOP 是什么有很多相互矛盾的定义,在一些定义下,Rust 是面向对象的;在其他定义下,Rust 不是。在本章节中,我们会探索一些被普遍认为是面向对象的特性和这些特性是如何体现在 Rust 语言习惯中的。接着会展示如何在 Rust 中实现面向对象设计模式,并讨论这么做与利用 Rust 自身的一些优势实现的方案相比有什么取舍。
|
[ch17-00]: https://github.com/rust-lang/book/blob/master/second-edition/src/ch17-00-oop.md
|
||||||
|
[commit]: https://github.com/rust-lang/book/commit/07b0ca8c829af09d60ab4eb9e69584b6f4a96f60
|
||||||
|
|
||||||
|
面向对象编程(Object-Oriented Programming,OOP)
|
||||||
|
|
||||||
|
面向对象编程(Object-Oriented Programming,OOP)是一种起源于 20 世纪 60 年代的 Simula 编程语言的模式化编程方式,然后在 90 年代随着 C++ 语言开始流行。关于 OOP 是什么有很多相互矛盾的定义,在一些定义下,Rust 是面向对象的;在其他定义下,Rust 不是。在本章节中,我们会探索一些被普遍认为是面向对象的特性和这些特性是如何体现在 Rust 语言习惯中的。接着会展示如何在 Rust 中实现面向对象设计模式,并讨论这么做与利用 Rust 自身的一些优势实现的方案相比有什么取舍。
|
||||||
|
@ -18,10 +18,6 @@ had this to say about it https://www.martinfowler.com/bliki/GangOfFour.html:
|
|||||||
|
|
||||||
`Design Patterns: Elements of Reusable Object-Oriented Software` 这本书被俗称为 `The Gang of Four book`,是面向对象编程模式的目录。它这样定义面向对象编程:
|
`Design Patterns: Elements of Reusable Object-Oriented Software` 这本书被俗称为 `The Gang of Four book`,是面向对象编程模式的目录。它这样定义面向对象编程:
|
||||||
|
|
||||||
> Object-oriented programs are made up of objects. An *object* packages both
|
|
||||||
> data and the procedures that operate on that data. The procedures are
|
|
||||||
> typically called *methods* or *operations*.
|
|
||||||
>
|
|
||||||
> 面向对象的程序是由对象组成的。一个 **对象** 包含数据和操作这些数据的过程。这些过程通常被称为 **方法** 或 **操作**。
|
> 面向对象的程序是由对象组成的。一个 **对象** 包含数据和操作这些数据的过程。这些过程通常被称为 **方法** 或 **操作**。
|
||||||
|
|
||||||
在这个定义下,Rust 是面向对象的:结构体和枚举包含数据而 impl 块提供了在结构体和枚举之上的方法。虽然带有方法的结构体和枚举并不被 **称为** 对象,但是他们提供了与对象相同的功能,参考 Gang of Four 中对象的定义。
|
在这个定义下,Rust 是面向对象的:结构体和枚举包含数据而 impl 块提供了在结构体和枚举之上的方法。虽然带有方法的结构体和枚举并不被 **称为** 对象,但是他们提供了与对象相同的功能,参考 Gang of Four 中对象的定义。
|
||||||
|
Loading…
Reference in New Issue
Block a user