Skip to content

Commit 030f90a

Browse files
author
wayslog
committed
Merge branch 'master' of github.com:rustcc/RustPrimer
2 parents 4f240be + 58684cd commit 030f90a

36 files changed

Lines changed: 434 additions & 396 deletions

03-editors/03-02-vim.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,36 @@ let g:racer_cmd = "<path-to-racer>/target/release/racer"
7676
let $RUST_SRC_PATH="<path-to-rust-srcdir>/src/"
7777
```
7878

79+
#### 使用 YouCompleteMe
80+
81+
YouCompleteMe 是 Vim 下的智能补全插件, 支持 C-family, Python, Rust 等的语法补全, 整合了多种插件, 功能强大. Linux 各发行版的官方源里基本都有软件包, 可直接安装. 如果有需要进行编译安装的话, 可参考[官方教程](https://github.com/Valloric/YouCompleteMe#installation)
82+
83+
让 YCM 支持 Rust 需要在安装 YCM 过程中执行 ./install.py 时加上 --racer-completer, 并在 .vimrc 中添加如下设置
84+
```
85+
let g:ycm_rust_src_path="<path-to-rust-srcdir>/src/"
86+
"" 一些方便的快捷键
87+
""" 在 Normal 模式下, 敲 <leader>jd 跳转到定义或声明(支持跨文件)
88+
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
89+
""" 在 Insert 模式下, 敲 <leader>; 补全
90+
inoremap <leader>; <C-x><C-o>
91+
```
92+
7993
## 1.3.1 总结
8094

81-
经过不多的配置,我们的到了包括如下功能
95+
经过不多的配置,我们得到了如下功能
8296

83-
1. 基本的c-x c-o补全
97+
1. 基本的c-x c-o补全 (使用 YCM 后, 能做到自动补全)
8498
2. 语法着色
85-
3. gd跳转到定义(不好用)
99+
3. gd跳转到定义
86100

87101
总体来看支持度并不高。
88102

89103
![此处应该有第二张截图](../image/03-editor-vim-welldone.png)
90104

91105
### 1.3.1 额外的
92-
Q1. 为什么没有自动补全
93-
94-
A1. 事实上我是一直在用YCM来进行补全的,但是racer并没有被YCM支持,而且似乎有人去YCM下提ISSUE,但是被作者拒绝了,于是就呵呵了。
95-
96-
Q2. 颜色好搓
106+
Q1. 颜色好搓
97107

98-
A2. 我推荐一个配色,也是我自己用的 [molokai](https://github.com/tomasr/molokai)
108+
A1. 我推荐一个配色,也是我自己用的 [molokai](https://github.com/tomasr/molokai)
99109

100110
更详细内容可以参见我的[vimrc配置](https://github.com/wayslog/dotfiles/blob/master/_vimrc),当然,我这个用的是比较老的版本的vundle,仅供参考。
101111

06-flow/06-01-comment.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Rust 代码文件中,通常我们可以看到 3 种注释。
1010

1111
使用 `//` 进行行注释。
1212

13-
```
13+
```rust
1414
// 创建一个绑定
1515
let x = 5;
1616

@@ -21,22 +21,23 @@ let y = 6; // 创建另一个绑定
2121

2222
文档注释使用 ```///```,一般用于函数或结构体(字段)的说明,置于要说明的对象上方。文档注释内部可使用markdown格式的标记语法,可用于 rustdoc 工具的自动文档提取。
2323

24-
/// Adds one to the number given.
25-
///
26-
/// # Examples
27-
///
28-
/// ```
29-
/// let five = 5;
30-
///
31-
/// assert_eq!(6, add_one(5));
32-
/// # fn add_one(x: i32) -> i32 {
33-
/// # x + 1
34-
/// # }
35-
/// ```
36-
fn add_one(x: i32) -> i32 {
37-
x + 1
38-
}
39-
24+
```rust
25+
/// Adds one to the number given.
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// let five = 5;
31+
///
32+
/// assert_eq!(6, add_one(5));
33+
/// # fn add_one(x: i32) -> i32 {
34+
/// # x + 1
35+
/// # }
36+
/// ```
37+
fn add_one(x: i32) -> i32 {
38+
x + 1
39+
}
40+
```
4041

4142
## 模块注释
4243

06-flow/06-02-condition.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88

99
Rust 中的 if 表达式基本就是如下几种形式:
1010

11-
```
11+
```rust
1212
// 形式 1
1313
if expr1 {
1414

1515
}
16-
16+
1717
// 形式 2
1818
if expr1 {
1919

2020
}
2121
else {
22-
22+
2323
}
2424

2525
// 形式 3
2626
if expr1 {
27-
27+
2828
}
2929
else if expr2 {
3030
// else if 可多重
3131
}
3232
else {
33-
33+
3434
}
3535

3636
```
@@ -42,7 +42,7 @@ else {
4242

4343
鉴于上述第二点,因为是表达式,所以我们可以写出如下代码:
4444

45-
```
45+
```rust
4646
let x = 5;
4747

4848
let y = if x == 5 {
@@ -54,17 +54,17 @@ let y = if x == 5 {
5454

5555
或者压缩成一行:
5656

57-
```
57+
```rust
5858
let x = 5;
5959

6060
let y = if x == 5 { 10 } else { 15 }; // y: i32
6161
```
6262

63-
## if let
63+
## if let
6464

6565
我们在代码中常常会看到 `if let` 成对出现,这实际上是一个 match 的简化用法。直接举例来说明:
6666

67-
```
67+
```rust
6868
let x = Some(5);
6969

7070
if let Some(y) = x {
@@ -76,14 +76,14 @@ let z = if let Some(y) = x {
7676
}
7777
else {
7878
0
79-
}
79+
};
8080
// z 值为 5
8181

8282
```
8383

84-
上面代码等价于
84+
上面代码等价于
8585

86-
```
86+
```rust
8787
let x = Some(5);
8888
match x {
8989
Some(y) => println!("{}", y),
@@ -93,7 +93,7 @@ match x {
9393
let z = match x {
9494
Some(y) => y,
9595
None => 0
96-
}
96+
};
9797
```
9898

9999
设计这个语法的目的是,在条件判断的时候,直接做一次模式匹配,方便代码书写,使代码更紧凑。
@@ -104,7 +104,7 @@ Rust 中没有类似于 C 的 `switch` 关键字,但它有用于模式匹配
104104

105105
match 的使用非常简单,举例如下:
106106

107-
```
107+
```rust
108108
let x = 5;
109109

110110
match x {
@@ -119,4 +119,3 @@ match x {
119119
}
120120
```
121121
注意,match 也是一个表达式。match 后面会专门论述,请参见 **模式匹配** 这一章。
122-

06-flow/06-03-repeatition.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
## for
1111

1212
for 语句用于遍历一个迭代器。
13-
```
13+
```rust
1414
for var in iterator {
1515
code
1616
}
1717
```
1818
Rust 迭代器返回一系列的元素,每个元素是循环中的一次重复。然后它的值与 var 绑定,它在循环体中有效。每当循环体执行完后,我们从迭代器中取出下一个值,然后我们再重复一遍。当迭代器中不再有值时,for 循环结束。
1919

2020
比如:
21-
```
21+
```rust
2222
for x in 0..10 {
2323
println!("{}", x); // x: i32
2424
}
@@ -39,7 +39,7 @@ for x in 0..10 {
3939

4040
不熟悉迭代器概念的同学可能傻眼了,下面不仿用 C 形式的 for 语句做下对比:
4141

42-
```
42+
```rust
4343
// C 语言的 for 循环例子
4444
for (x = 0; x < 10; x++) {
4545
printf( "%d\n", x );
@@ -57,7 +57,7 @@ for 语句就是迭代器遍历的语法糖。
5757

5858
上述迭代器的形式虽好,但是好像在循环过程中,少了索引信息。Rust 考虑到了这一点,当你需要记录你已经循环了多少次了的时候,你可以使用 `.enumerate()` 函数。比如:
5959

60-
```
60+
```rust
6161
for (i,j) in (5..10).enumerate() {
6262
println!("i = {} and j = {}", i, j);
6363
}
@@ -73,7 +73,7 @@ i = 4 and j = 9
7373
```
7474
再比如:
7575

76-
```
76+
```rust
7777
let lines = "Content of line one\nContent of line two\nContent of line three\nContent of line four".lines();
7878
for (linenumber, line) in lines.enumerate() {
7979
println!("{}: {}", linenumber, line);
@@ -93,15 +93,15 @@ for (linenumber, line) in lines.enumerate() {
9393

9494
Rust 提供了 while 语句,条件表达式为真时,执行语句体。当你不确定应该循环多少次时可选择 while。
9595

96-
```
96+
```rust
9797
while expression {
9898
code
9999
}
100100
```
101101

102102
比如:
103103

104-
```
104+
```rust
105105
let mut x = 5; // mut x: i32
106106
let mut done = false; // mut done: bool
107107

@@ -120,15 +120,15 @@ while !done {
120120

121121
有一种情况,我们经常会遇到,就是写一个无限循环:
122122

123-
```
123+
```rust
124124
while true {
125125
// do something
126126
}
127127
```
128128

129129
针对这种情况,Rust 专门优化提供了一个语句 loop。
130130

131-
```
131+
```rust
132132
loop {
133133
// do something
134134
}
@@ -138,7 +138,7 @@ loop {
138138

139139
比如说,如下代码:
140140

141-
```
141+
```rust
142142
let mut a;
143143
loop {
144144
a = 1;
@@ -158,7 +158,7 @@ do_something(a)
158158

159159
像上面那个 while 例子:
160160

161-
```
161+
```rust
162162
let mut x = 5;
163163
let mut done = false;
164164

@@ -174,7 +174,7 @@ while !done {
174174
```
175175
可以优化成:
176176

177-
```
177+
```rust
178178
let mut x = 5;
179179

180180
loop {
@@ -189,7 +189,7 @@ loop {
189189

190190
下面这个例子演示 continue 的用法:
191191

192-
```
192+
```rust
193193
for x in 0..10 {
194194
if x % 2 == 0 { continue; }
195195

@@ -212,7 +212,7 @@ for x in 0..10 {
212212

213213
如下代码只会在 x 和 y 都为奇数时打印他们:
214214

215-
```
215+
```rust
216216
'outer: for x in 0..10 {
217217
'inner: for y in 0..10 {
218218
if x % 2 == 0 { continue 'outer; } // continues the loop over x
@@ -221,4 +221,3 @@ for x in 0..10 {
221221
}
222222
}
223223
```
224-

0 commit comments

Comments
 (0)