2017-03-02 23:36:03 +08:00
<!DOCTYPE HTML>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title > 测试库的功能 - Rust 程序设计语言 简体中文版< / title >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "Rust 程序设计语言 简体中文版" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< base href = "" >
< link rel = "stylesheet" href = "book.css" >
< link href = 'https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel = 'stylesheet' type = 'text/css' >
< link rel = "shortcut icon" href = "favicon.png" >
<!-- Font Awesome -->
< link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" >
< link rel = "stylesheet" href = "highlight.css" >
< link rel = "stylesheet" href = "tomorrow-night.css" >
<!-- MathJax -->
< script type = "text/javascript" src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" > < / script >
<!-- Fetch JQuery from CDN but have a local fallback -->
< script src = "https://code.jquery.com/jquery-2.1.4.min.js" > < / script >
< script >
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
}
< / script >
< / head >
< body class = "light" >
<!-- Set the theme before any content is loaded, prevents flash -->
< script type = "text/javascript" >
var theme = localStorage.getItem('theme');
if (theme == null) { theme = 'light'; }
$('body').removeClass().addClass(theme);
< / script >
<!-- Hide / unhide sidebar before it is displayed -->
< script type = "text/javascript" >
var sidebar = localStorage.getItem('sidebar');
if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
< / script >
< div id = "sidebar" class = "sidebar" >
2017-04-05 23:13:49 +08:00
< ul class = "chapter" > < li > < a href = "ch01-00-introduction.html" > < strong > 1.< / strong > 介绍< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch01-01-installation.html" > < strong > 1.1.< / strong > 安装< / a > < / li > < li > < a href = "ch01-02-hello-world.html" > < strong > 1.2.< / strong > Hello, World!< / a > < / li > < / ul > < / li > < li > < a href = "ch02-00-guessing-game-tutorial.html" > < strong > 2.< / strong > 猜猜看教程< / a > < / li > < li > < a href = "ch03-00-common-programming-concepts.html" > < strong > 3.< / strong > 通用编程概念< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch03-01-variables-and-mutability.html" > < strong > 3.1.< / strong > 变量和可变性< / a > < / li > < li > < a href = "ch03-02-data-types.html" > < strong > 3.2.< / strong > 数据类型< / a > < / li > < li > < a href = "ch03-03-how-functions-work.html" > < strong > 3.3.< / strong > 函数如何工作< / a > < / li > < li > < a href = "ch03-04-comments.html" > < strong > 3.4.< / strong > 注释< / a > < / li > < li > < a href = "ch03-05-control-flow.html" > < strong > 3.5.< / strong > 控制流< / a > < / li > < / ul > < / li > < li > < a href = "ch04-00-understanding-ownership.html" > < strong > 4.< / strong > 认识所有权< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch04-01-what-is-ownership.html" > < strong > 4.1.< / strong > 什么是所有权< / a > < / li > < li > < a href = "ch04-02-references-and-borrowing.html" > < strong > 4.2.< / strong > 引用 & 借用< / a > < / li > < li > < a href = "ch04-03-slices.html" > < strong > 4.3.< / strong > Slices< / a > < / li > < / ul > < / li > < li > < a href = "ch05-00-structs.html" > < strong > 5.< / strong > 结构体< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch05-01-method-syntax.html" > < strong > 5.1.< / strong > 方法语法< / a > < / li > < / ul > < / li > < li > < a href = "ch06-00-enums.html" > < strong > 6.< / strong > 枚举和模式匹配< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch06-01-defining-an-enum.html" > < strong > 6.1.< / strong > 定义枚举< / a > < / li > < li > < a href = "ch06-02-match.html" > < strong > 6.2.< / strong > < code > match< / code > 控制流运算符< / a > < / li > < li > < a href = "ch06-03-if-let.html" > < strong > 6.3.< / strong > < code > if let< / code > 简单控制流< / a > < / li > < / ul > < / li > < li > < a href = "ch07-00-modules.html" > < strong > 7.< / strong > 模块< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch07-01-mod-and-the-filesystem.html" > < strong > 7.1.< / strong > < code > mod< / code > 和文件系统< / a > < / li > < li > < a href = "ch07-02-controlling-visibility-with-pub.html" > < strong > 7.2.< / strong > 使用< code > pub< / code > 控制可见性< / a > < / li > < li > < a href = "ch07-03-importing-names-with-use.html" > < strong > 7.3.< / strong > 使用< code > use< / code > 导入命名< / a > < / li > < / ul > < / li > < li > < a href = "ch08-00-common-collections.html" > < strong > 8.< / strong > 通用集合类型< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch08-01-vectors.html" > < strong > 8.1.< / strong > vector< / a > < / li > < li > < a href = "ch08-02-strings.html" > < strong > 8.2.< / strong > 字符串< / a > < / li > < li > < a href = "ch08-03-hash-maps.html" > < strong > 8.3.< / strong > 哈希 map< / a > < / li > < / ul > < / li > < li > < a href = "ch09-00-error-handling.html" > < strong > 9.< / strong > 错误处理< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch09-01-unrecoverable-errors-with-panic.html" > < strong > 9.1.< / strong > < code > panic!< / code > 与不可恢复的错误< / a > < / li > < li > < a href = "ch09-02-recoverable-errors-with-result.html" > < strong > 9.2.< / strong > < code > Result< / code > 与可恢复的错误< / a > < / li > < li > < a href = "ch09-03-to-panic-or-not-to-panic.html" > < strong > 9.3.< / strong > < code > panic!< / code > 还是不< code > panic!< / code > < / a > < / li > < / ul > < / li > < li > < a href = "ch10-00-generics.html" > < strong > 10.< / strong > 泛型、trait 和生命周期< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch10-01-syntax.html" > < strong > 10.1.< / strong > 泛型数据类型< / a > < / li > < li > < a href = "ch10-02-traits.html" > < strong > 10.2.< / strong > trait: 定义共享的行为< / a > < / li > < li > < a href = "ch10-03-lifetime-syntax.html" > < strong > 10.3.< / strong > 生命周期与引用有效性< / a > < / li > < / ul > < / li > < li > < a href = "ch11-00-testing.html" > < strong > 11.< / strong > 测试< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch11-01-writing-tests.html" > < strong > 11.1.< / strong > 编写测试< / a > < / li > < li > < a href = "ch11-02-running-tests.html" > < strong > 11.2.< / strong > 运行测试< / a > < / li > < li > < a href = "ch11-03-test-organization.html" > < strong > 11.3.< / strong > 测试的组织结构< / a > < / li > < / ul > < / li > < li > < a href = "ch12-00-an-io-project.h
2017-03-02 23:36:03 +08:00
< / div >
< div id = "page-wrapper" class = "page-wrapper" >
< div class = "page" >
< div id = "menu-bar" class = "menu-bar" >
< div class = "left-buttons" >
< i id = "sidebar-toggle" class = "fa fa-bars" > < / i >
< i id = "theme-toggle" class = "fa fa-paint-brush" > < / i >
< / div >
< h1 class = "menu-title" > Rust 程序设计语言 简体中文版< / h1 >
< div class = "right-buttons" >
< i id = "print-button" class = "fa fa-print" title = "Print this book" > < / i >
< / div >
< / div >
< div id = "content" class = "content" >
2017-03-06 22:56:55 +08:00
< a class = "header" href = "#测试库的功能" name = "测试库的功能" > < h2 > 测试库的功能< / h2 > < / a >
< blockquote >
< p > < a href = "https://github.com/rust-lang/book/blob/master/second-edition/src/ch12-04-testing-the-librarys-functionality.md" > ch12-04-testing-the-librarys-functionality.md< / a >
< br >
2017-03-27 16:17:20 +08:00
commit 3f2a1bd8dbb19cc48b210fc4fb35c305c8d81b56< / p >
2017-03-06 22:56:55 +08:00
< / blockquote >
< p > 现在为项目的核心功能编写测试将更加容易,因为我们将逻辑提取到了 < em > src/lib.rs< / em > 中并将参数解析和错误处理都留在了 < em > src/main.rs< / em > 里。现在我们可以直接使用多种参数调用代码并检查返回值而不用从命令行运行二进制文件了。< / p >
< p > 我们将要编写的是一个叫做< code > grep< / code > 的函数,它获取要搜索的项以及文本并产生一个搜索结果列表。让我们从< code > run< / code > 中去掉那行< code > println!< / code > (也去掉 < em > src/main.rs< / em > 中的,因为再也不需要他们了),并使用之前收集的选项来调用新的< code > grep< / code > 函数。眼下我们只增加一个空的实现,和指定< code > grep< / code > 期望行为的测试。当然,这个测试对于空的实现来说是会失败的,不过可以确保代码是可以编译的并得到期望的错误信息。列表 12-14 展示了这些修改:< / p >
2017-03-27 16:17:20 +08:00
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
2017-03-06 22:56:55 +08:00
< pre > < code class = "language-rust" > # use std::error::Error;
# use std::fs::File;
# use std::io::prelude::*;
#
# pub struct Config {
# pub search: String,
# pub filename: String,
# }
#
// ...snip...
fn grep< 'a> (search: & str, contents: & 'a str) -> Vec< & 'a str> {
vec![]
}
pub fn run(config: Config) -> Result< (), Box< Error> > {
let mut f = File::open(config.filename)?;
let mut contents = String::new();
f.read_to_string(& mut contents)?;
grep(& config.search, & contents);
Ok(())
}
#[cfg(test)]
mod test {
use grep;
#[test]
fn one_result() {
let search = " duct" ;
let contents = " \
Rust:
safe, fast, productive.
Pick three." ;
assert_eq!(
vec![" safe, fast, productive." ],
grep(search, contents)
);
}
}
< / code > < / pre >
2017-03-27 16:17:20 +08:00
< p > < span class = "caption" > Listing 12-14: Creating a function where our logic will
go and a failing test for that function< / span > < / p >
2017-03-06 22:56:55 +08:00
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
< p > 注意需要在< code > grep< / code > 的签名中显式声明声明周期< code > 'a< / code > 并用于< code > contents< / code > 参数和返回值。记住,生命周期参数用于指定函数参数于返回值的生命周期的关系。在这个例子中,我们表明返回的 vector 将包含引用参数< code > contents< / code > 的字符串 slice, 而不是引用参数< code > search< / code > 的字符串 slice。换一种说法就是我们告诉 Rust 函数< code > grep< / code > 返回的数据将和传递给它的参数< code > contents< / code > 的数据存活的同样久。这是非常重要的!考虑为了使引用有效则 slice 引用的数据也需要保持有效,如果编译器认为我们是在创建< code > search< / code > 而不是< code > contents< / code > 的 slice, 那么安全检查将是不正确的。如果尝试不用生命周期编译的话, 我们将得到如下错误: < / p >
< pre > < code > error[E0106]: missing lifetime specifier
--> src\lib.rs:37:46
|
37 | fn grep(search: & str, contents: & str) -> Vec< & str> {
| ^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but the
signature does not say whether it is borrowed from `search` or
`contents`
< / code > < / pre >
< p > Rust 不可能知道我们需要的是哪一个参数,所以需要告诉它。因为参数< code > contents< / code > 包含了所有的文本而且我们希望返回匹配的那部分文本,而我们知道< code > contents< / code > 是应该要使用生命周期语法来与返回值相关联的参数。< / p >
< p > 在函数签名中将参数与返回值相关联是其他语言不会让你做的工作,所以不用担心这感觉很奇怪!掌握如何指定生命周期会随着时间的推移越来越容易,熟能生巧。你可能想要重新阅读上一部分或返回与第十章中生命周期语法部分的例子做对比。< / p >
< p > 现在试试运行测试:< / p >
< pre > < code > $ cargo test
...warnings...
Finished debug [unoptimized + debuginfo] target(s) in 0.43 secs
Running target/debug/deps/greprs-abcabcabc
running 1 test
test test::one_result ... FAILED
failures:
---- test::one_result stdout ----
thread 'test::one_result' panicked at 'assertion failed: `(left == right)`
(left: `[" safe, fast, productive." ]`, right: `[]`)', src/lib.rs:16
note: Run with `RUST_BACKTRACE=1` for a backtrace.
failures:
test::one_result
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
error: test failed
< / code > < / pre >
< p > 好的,测试失败了,这正是我们所期望的。修改代码来让测试通过吧!之所以会失败是因为我们总是返回一个空的 vector。如下是如何实现< code > grep< / code > 的步骤:< / p >
< ol >
< li > 遍历每一行文本。< / li >
< li > 查看这一行是否包含要搜索的字符串。
< ul >
< li > 如果有,将这一行加入返回列表中< / li >
< li > 如果没有,什么也不做< / li >
< / ul >
< / li >
< li > 返回匹配到的列表< / li >
< / ol >
< p > 让我们一步一步的来,从遍历每行开始。字符串类型有一个有用的方法来处理这种情况,它刚好叫做< code > lines< / code > : < / p >
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > fn grep< 'a> (search: & str, contents: & 'a str) -> Vec< & 'a str> {
for line in contents.lines() {
// do something with line
}
}
< / code > < / pre >
<!-- Will add wingdings in libreoffice /Carol -->
< p > 我们使用了一个< code > for< / code > 循环和< code > lines< / code > 方法来依次获得每一行。接下来,让我们看看这些行是否包含要搜索的字符串。幸运的是,字符串类型为此也有一个有用的方法< code > contains< / code > ! < code > contains< / code > 的用法看起来像这样:< / p >
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > fn grep< 'a> (search: & str, contents: & 'a str) -> Vec< & 'a str> {
for line in contents.lines() {
if line.contains(search) {
// do something with line
}
}
}
< / code > < / pre >
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
< p > 最终,我们需要一个方法来存储包含要搜索字符串的行。为此可以在< code > for< / code > 循环之前创建一个可变的 vector 并调用< code > push< / code > 方法来存放一个< code > line< / code > 。在< code > for< / code > 循环之后,返回这个 vector。列表 12-15 中为完整的实现:< / p >
2017-03-27 16:17:20 +08:00
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
2017-03-06 22:56:55 +08:00
< pre > < code class = "language-rust" > fn grep< 'a> (search: & str, contents: & 'a str) -> Vec< & 'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(search) {
results.push(line);
}
}
results
}
< / code > < / pre >
2017-03-27 16:17:20 +08:00
< p > < span class = "caption" > Listing 12-15: Fully functioning implementation of the
< code > grep< / code > function< / span > < / p >
2017-03-06 22:56:55 +08:00
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
< p > 尝试运行一下:< / p >
< pre > < code > $ cargo test
running 1 test
test test::one_result ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
Running target/debug/greprs-2f55ee8cd1721808
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
Doc-tests greprs
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
< / code > < / pre >
< p > 非常好!它可以工作了。现在测试通过了,我们可以考虑一下重构< code > grep< / code > 的实现并时刻保持其功能不变。这些代码并不坏,不过并没有利用迭代器的一些实用功能。第十三章将回到这个例子并探索迭代器和如何改进代码。< / p >
< p > 现在< code > grep< / code > 函数是可以工作的,我们还需在在< code > run< / code > 函数中做最后一件事:还没有打印出结果呢!增加一个< code > for< / code > 循环来打印出< code > grep< / code > 函数返回的每一行:< / p >
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > pub fn run(config: Config) -> Result< (), Box< Error> > {
let mut f = File::open(config.filename)?;
let mut contents = String::new();
f.read_to_string(& mut contents)?;
for line in grep(& config.search, & contents) {
println!(" {}" , line);
}
Ok(())
}
< / code > < / pre >
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
< p > 现在程序应该能正常运行了!试试吧:< / p >
< pre > < code > $ cargo run the poem.txt
Compiling greprs v0.1.0 (file:///projects/greprs)
Finished debug [unoptimized + debuginfo] target(s) in 0.38 secs
Running `target\debug\greprs.exe the poem.txt`
Then there's a pair of us - don't tell!
To tell your name the livelong day
$ cargo run a poem.txt
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target\debug\greprs.exe a poem.txt`
I'm nobody! Who are you?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!
< / code > < / pre >
< p > 好极了!我们创建了一个属于自己的经典工具,并学习了很多如何组织程序的知识。我们还学习了一些文件输入输出、生命周期、测试和命令行解析的内容。< / p >
2017-03-02 23:36:03 +08:00
< / div >
<!-- Mobile navigation buttons -->
< a href = "ch12-03-improving-error-handling-and-modularity.html" class = "mobile-nav-chapters previous" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch12-05-working-with-environment-variables.html" class = "mobile-nav-chapters next" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
< a href = "ch12-03-improving-error-handling-and-modularity.html" class = "nav-chapters previous" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch12-05-working-with-environment-variables.html" class = "nav-chapters next" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
<!-- Local fallback for Font Awesome -->
< script >
if ($(".fa").css("font-family") !== "FontAwesome") {
$('< link rel = "stylesheet" type = "text/css" href = "_FontAwesome/css/font-awesome.css" > ').prependTo('head');
}
< / script >
<!-- Livereload script (if served using the cli tool) -->
< script src = "highlight.js" > < / script >
< script src = "book.js" > < / script >
< / body >
< / html >