File tree Expand file tree Collapse file tree 4 files changed +12
-8
lines changed Expand file tree Collapse file tree 4 files changed +12
-8
lines changed Original file line number Diff line number Diff line change @@ -33,7 +33,9 @@ fn func_with_return_value_mod(i: i32, j: i32) -> i32 {
33
33
!!! caution "函数返回值"
34
34
35
35
无返回值的函数会默认返回 **单位元** `()`,并非真实语义下的“无返回值”。
36
+
36
37
Rust 编译器具有严格的类型检查,如果函数指定了返回值类型,但没有给出返回值或 return 语句,编译器将给出 `mismatched types` 报错。
38
+
37
39
类似的,如果没给定返回值类型,但返回了 **非单位元** 类型的值,编译器也会给出 `mismatched types` 报错。
38
40
39
41
## 函数调用
Original file line number Diff line number Diff line change 21
21
## 资源链接
22
22
23
23
### 官方参考资料
24
- - The Rust Programming Language(也被称为 The Book)。 Rust 官方网站推荐的 Rust 语言入门书籍 https://doc.rust-lang.org/book/
24
+ - The Rust Programming Language(也被称为 The Book): Rust 官方网站推荐的 Rust 语言入门书籍 https://doc.rust-lang.org/book/
25
25
- The Book 的中文社区翻译版,更新很及时 https://kaisery.github.io/trpl-zh-cn/
26
26
27
- - Rustlings。 Rust 官方推荐的另一种入门方式,适合喜欢边学边做的同学 https://github.com/rust-lang/rustlings/
27
+ - Rustlings: Rust 官方推荐的另一种入门方式,适合喜欢边学边做的同学 https://github.com/rust-lang/rustlings/
28
28
29
29
### 入门教程
30
30
- Rust 官方文档中文教程。https://rustwiki.org
31
31
32
32
- RUNOOB.COM 的 Rust 教程:简明的入门教程,可作为第一份学习资料。https://www.runoob.com/rust/rust-tutorial.html
33
33
34
34
### 阅读材料
35
- - Effective Rust。 Rust 进阶教程,比较详细地介绍了 Rust 中类型(Types)、特型(Trait)及 Rust 语言的设计理念和使用方法。 https://www.lurklurk.org/effective-rust/
35
+ - Effective Rust: Rust 进阶教程,比较详细地介绍了 Rust 中类型(Types)、特型(Trait)及 Rust 语言的设计理念和使用方法。 https://www.lurklurk.org/effective-rust/
36
36
37
37
### 课程材料
38
38
- CIS 198: Rust Programming:宾夕法尼亚大学的 Rust 课程。https://cis198-2016s.github.io
39
39
40
- - 清华大学程序设计训练(Rust)文档: https://lab.cs.tsinghua.edu.cn/rust/
40
+ - 清华大学程序设计训练(Rust)文档。 https://lab.cs.tsinghua.edu.cn/rust/
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ let coin = Coin {
23
23
```
24
24
25
25
枚举的每种变体 (variant) 可以:
26
+
26
27
- 没有数据(单位元变体)
27
28
- 有命名的数据域(结构体变体)
28
29
- 有不命名的有序数据域(元组变体)
Original file line number Diff line number Diff line change 3
3
为了兼顾内存使用的** 安全** 和** 性能** ,Rust 在设计之初就采用了与 C++ 完全不同的内存管理。
4
4
5
5
Rust 引入了** 所有权(ownership)** 的概念:
6
+
6
7
- Rust 中的每个值都有所有者 (owner)。
7
8
- 同一时刻每个值只有一个所有者。
8
9
- 当所有者失效,值也将被丢弃。
@@ -58,8 +59,8 @@ fn main() {
58
59
59
60
!!! note "借用规则"
60
61
- 不能在某个对象不存在后继续保留对它的引用。一个对象可以
61
- - 同时存在** 多个不可变** 引用(&T)
62
- - 或者** 仅有一个可变引用** (&mut T)。
62
+ - 同时存在** 多个不可变** 引用(&T)
63
+ - 或者** 仅有一个可变引用** (&mut T)。
63
64
- 以上两者不能同时存在
64
65
65
66
需要注意的是,** 函数** 的** 参数传递** 时,采用的是与变量赋值和借用相同的语法规则。如果不指定 ` & ` 或 ` &mut ` ,变量会直接发生移动。
@@ -89,7 +90,7 @@ fn main() {
89
90
如果想要直接获得一份变量的副本,可以使用 Rust 的 Copy 特型。
90
91
91
92
- 大多数基本类型是 Copy 类型(i32、f64、char、bool 等等)。
92
- - 基本类型发生赋值时,会发生拷贝而非移动。
93
+ - 基本类型发生赋值时,会发生拷贝而非移动。
93
94
94
95
95
96
```rust
@@ -99,7 +100,7 @@ fn main() {
99
100
```
100
101
101
102
- 可以通过 `impl Copy for ...` 或 `#[derive(Copy)]` 宏为变量实现 Copy 特型
102
- - 非基本类型在实现了 Copy 特型后,可以通过 `clone()` 等方法进行拷贝。
103
+ - 非基本类型在实现了 Copy 特型后,可以通过 `clone()` 等方法进行拷贝。
103
104
104
105
```rust
105
106
let x: String = String::from("hello");
You can’t perform that action at this time.
0 commit comments