Skip to content

Commit f5a5150

Browse files
authored
Merge pull request #36 from AmaiKinono/add/chapter12
翻译了第十二章
2 parents b1865ae + b1314bc commit f5a5150

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

src/SUMMARY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
- [](crates/lib.md)
8484
- [`extern crate`](crates/link.md)
8585

86+
- [cargo](cargo.md)
87+
- [依赖](cargo/deps.md)
88+
- [惯例](cargo/conventions.md)
89+
- [测试](cargo/test.md)
90+
8691
- [属性](attribute.md)
8792
- [死代码 `dead_code`](attribute/unused.md)
8893
- [`crate`](attribute/crate.md)

src/cargo.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Cargo
2+
3+
`cargo` 是官方的 Rust 包管理工具,它包含大量有用的特性,能够帮助你提高代码质量
4+
与生产力!包括:
5+
6+
- 依赖管理,以及和 [crates.io](https://crates.io) (Rust 包的官方登记处)的集成
7+
- 可检测到单元测试
8+
- 可检测到性能测试
9+
10+
本章将会快速地讲一些基础内容,你可以在[这里](http://doc.crates.io/index.html)找到
11+
完整文档。

src/cargo/conventions.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 惯例
2+
3+
在上一节中,我们看到过这样的目录树:
4+
5+
```txt
6+
foo
7+
├── Cargo.toml
8+
└── src
9+
└── main.rs
10+
```
11+
12+
假设咱们想让一个工程生成两个可执行文件,怎么办呢?
13+
14+
`cargo` 是支持这样做的。默认的可执行文件是从 `main.rs` 生成的,不过你可以在
15+
`bin/` 目录下添加其他的可执行文件来源:
16+
17+
```txt
18+
foo
19+
├── Cargo.toml
20+
└── src
21+
├── main.rs
22+
└── bin
23+
└── my_other_bin.rs
24+
```
25+
26+
为了让 `cargo` 编译和运行 `my_other_bin.rs`,我们需要使用 `cargo`
27+
`--bin my_other_bin` 选项,其中 `my_other_bin` 是要处理的可执行文件名。
28+
29+
除去额外的可执行文件,`cargo` 还支持性能评估(benchmark)、测试(test)和
30+
示例程序(example)。完整的功能介绍
31+
请看[这里](http://doc.crates.io/book/guide/project-layout.html)
32+
33+
在下一节,咱们会详细了解测试。

src/cargo/deps.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 依赖
2+
3+
大多数程序都要依赖一些库。如果你手工管理过依赖,你一定知道那是一件多么痛苦的
4+
事情。幸运的是,`cargo` 是 Rust 生态系统的标配!`cargo` 能够管理一个工程的依赖。
5+
6+
使用以下命令可以新建工程:
7+
8+
```sh
9+
# 目标是二进制文件
10+
cargo new --bin foo
11+
12+
# 或者是库
13+
cargo new foo
14+
```
15+
16+
在本章接下来的部分,我会假设我们在编写可执行的应用,而不是库,不过它们所有的概念
17+
都是相同的。
18+
19+
在执行上述命令后,你应该会看到:
20+
21+
```txt
22+
foo
23+
├── Cargo.toml
24+
└── src
25+
└── main.rs
26+
```
27+
28+
`main.rs` 是你的新工程的根源文件(root source file),这里没什么新奇的。`Cargo.toml`
29+
是该工程(`foo`)的 `cargo` 配置文件。如果你打开看看,会看到类似这样的内容:
30+
31+
```toml
32+
[package]
33+
name = "foo"
34+
version = "0.1.0"
35+
authors = ["mark"]
36+
37+
[dependencies]
38+
```
39+
40+
你可以在[这里](http://doc.crates.io/manifest.html)找到所有可用的配置选项。
41+
42+
`package` 下面的 `name` 字段决定了工程的名字。如果你在 `crates.io` 上发布
43+
`crate`(详见后文),就会使用这个名字。这也是编译产生的二进制文件的名字。
44+
45+
`version` 字段是 crate 的版本号,遵循 [Semantic Versioning](http://semver.org/).
46+
47+
`authors` 字段是发布 crate 时所用的作者列表。
48+
49+
`dependencies` 部分用来添加工程的依赖。
50+
51+
比如说,我想让我的程序拥有一个好看的字符界面(CLI),那我可以在
52+
[crates.io](https://crates.io) 上找到不少这种库。[clap](https://crates.io/crates/clap)
53+
是比较流行的一个。在本书写作时,`clap` 最近的版本是 `2.27.1`。要给工程加上这个
54+
依赖,只需在 `Cargo.toml``dependencies` 下面加上 `clap = "2.27.1"`。当然,还
55+
需要在 `main.rs` 中写上 `extern crate clap`,这和以前一样。这样就完成了!接下来
56+
你可以在程序中使用 `clap` 了。
57+
58+
`cargo` 也支持其他的依赖来源。下面是一些例子,你可以
59+
[这里](http://doc.crates.io/specifying-dependencies.html)找到更多。
60+
61+
62+
```toml
63+
[package]
64+
name = "foo"
65+
version = "0.1.0"
66+
authors = ["mark"]
67+
68+
[dependencies]
69+
clap = "2.27.1" # 来自 crates.io
70+
rand = { git = "https://github.com/rust-lang-nursery/rand" } # 来自在线仓库
71+
bar = { path = "../bar" } # 来自本地文件系统的一个路径
72+
```
73+
74+
要构建工程,可以在工程的任何路径(包括子路径!)运行 `cargo build`。也可以运行
75+
`cargo run` 来构建 + 运行。注意这些命令会解决所有依赖问题,下载 crate(如果
76+
需要的话),然后构建它们和你的 `crate`(注意它只会构建那些还没有构建的东西,和
77+
`make` 一样)。
78+
79+
好了,这节就学到这里!

src/cargo/test.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 测试
2+
3+
众所周知,任何软件都需要测试!Rust 对单元测试和集成测试提供了完整的支持(请看 TRPL
4+
中的[这一章](https://doc.rust-lang.org/book/second-edition/ch11-00-testing.html))。
5+
6+
从上面链接的章节中,我们看到了如何编写单元测试和继承测试。我们可以很条理地把单元
7+
测试放到它们要测试的模块目录,把集成测试放到单独的 `tests/` 文件夹。
8+
9+
```txt
10+
foo
11+
├── Cargo.toml
12+
├── src
13+
│ └── main.rs
14+
└── tests
15+
├── my_test.rs
16+
└── my_other_test.rs
17+
```
18+
19+
这里 `tests` 文件夹中的每个文件都是独立的集成测试。
20+
21+
`cargo` 提供了一种运行所有测试的简单方式!
22+
23+
```sh
24+
cargo test
25+
```
26+
27+
你会看到类似这样的输出:
28+
29+
```txt
30+
$ cargo test
31+
Compiling blah v0.1.0 (file:///nobackup/blah)
32+
Finished dev [unoptimized + debuginfo] target(s) in 0.89 secs
33+
Running target/debug/deps/blah-d3b32b97275ec472
34+
35+
running 3 tests
36+
test test_bar ... ok
37+
test test_baz ... ok
38+
test test_foo_bar ... ok
39+
test test_foo ... ok
40+
41+
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
42+
```
43+
44+
你也可以运行那些名字匹配某一模式的测试:
45+
46+
```sh
47+
cargo test test_foo
48+
```
49+
50+
```txt
51+
$ cargo test test_foo
52+
Compiling blah v0.1.0 (file:///nobackup/blah)
53+
Finished dev [unoptimized + debuginfo] target(s) in 0.35 secs
54+
Running target/debug/deps/blah-d3b32b97275ec472
55+
56+
running 2 tests
57+
test test_foo ... ok
58+
test test_foo_bar ... ok
59+
60+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out
61+
```
62+
63+
注意一点:cargo 可能会并行地运行多个测试,所以确保它们不会彼此竞争。比如,它们
64+
都往同一个文件里输出内容,那么你应该修改它们,让结果写到不同的文件中。

0 commit comments

Comments
 (0)