|
|
<!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">
|
|
|
<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.html"><strong>12.</strong> 一个 I/O 项目</a></li><li><ul class="section"><li><a href="ch12-01-accepting-command-line-arguments.html"><strong>12.1.</strong> 接受命令行参数</a></li><li><a href="ch12-02-reading-a-file.html"><strong>12.2.</strong> 读取文件</a></li><li><a href="ch12-03-improving-error-handling-and-modularity.html"><strong>12.3.</strong> 增强错误处理和模块化</a></li><li><a href="ch12-04-testing-the-librarys-functionality.html"><strong>12.4.</strong> 测试库的功能</a></li><li><a href="ch12-05-working-with-environment-variables.html" class="active"><strong>12.5.</strong> 处理环境变量</a></li><li><a href="ch12-06-writing-to-stderr-instead-of-stdout.html"><strong>12.6.</strong> 输出到<code>stderr</code>而不是<code>stdout</code></a></li></ul></li><li><a href="ch13-00-functional-features.html"><strong>13.</strong> Rust 中的函数式语言功能</a></li><li><ul class="section"><li><a href="ch13-01-closures.html"><strong>13.1.</strong> 闭包</a></li><li><a href="ch13-02-iterators.html"><strong>13.2.</strong> 迭代器</a></li><li><a href="ch13-03-improving-our-io-project.html"><strong>13.3.</strong> 改进 I/O 项目</a></li><li><a href="ch13-04-performance.html"><strong>13.4.</strong> 性能</a></li></ul></li><li><a href="ch14-00-more-about-cargo.html"><strong>14.</strong> 更多关于 Cargo 和 Crates.io</a></li><li><ul class="section"><li><a href="ch14-01-release-profiles.html"><strong>14.1.</strong> 发布配置</a></li><li><a href="ch14-02-publishing-to-crates-io.html"><strong>14.2.</strong> 将 crate 发布到 Crates.io</a></li><li><a href="ch14-03-cargo-workspaces.html"><strong>14.3.</strong> Cargo 工作空间</a></li><li><a href="ch14-04-installing-binaries.html"><strong>14.4.</strong> 使用<code>cargo install</code>从 Crates.io 安装文件</a></li><li><a href="ch14-05-extending-cargo.html"><strong>14.5.</strong> Cargo 自定义扩展命令</a></li></ul></li><li><a href="ch15-00-smart-pointers.html"><strong>15.</strong> 智能指针</a></li><li><ul class="section"><li><a href="ch15-01-box.html"><strong>15.1.</strong> <code>Box<T></code>用于已知大小的堆上数据</a></li><li><a href="ch15-02-deref.html"><strong>15.2.</strong> <code>Deref</code> Trait 允许通过引用访问数据</a></li><li><a href="ch15-03-drop.html"><strong>15.3.</strong> <code>Drop</code> Trait 运行清理代码</a></li><li><a href="ch15-04-rc.html"><strong>15.4.</strong> <code>Rc<T></code> 引用计数智能指针</a></li><li><a href="ch15-05-interior-mutability.html"><strong>15.5.</strong> <code>RefCell<T></code>和内部可变性模式</a></li><li><a href="ch15-06-reference-cycles.html"><strong>15.6.</strong> 引用循环和内存泄漏是安全的</a></li></ul></li><li><a href="ch16-00-concurrency.html"><strong>16.</strong> 无畏并发</a></li><li><ul class="section"><li><a href="ch16-01-threads.html"><strong>16.1.</strong> 线程</a></li><li><a href="ch16-02-message-passing.html"><strong>16.2.</strong> 消息传递</a></li><li><a href="ch16-03-shared-state.html"><strong>16.3.</strong> 共享状态</a></li><li><a href="ch16-04-extensible-concurrency-sync-and-send.html"><strong>16.4.</strong> 可扩展的并发:<code>Sync</code>和<code>Send</code></a></li></ul></li><li><a href="ch17-00-oop.html"><strong>17.</strong> 面向对象</a></li><li><ul class="section"><li><a href="ch17-01-what-is-oo.html"><strong>17.1.</strong> 什么是面向对象?</a></li><li><a href="ch17-02-trait-objects.html"><strong>17.2.</strong> 为使用不同类型的值而设计的 trait 对象</a></li><li><a href="ch17-03-oo-design-patterns.html"><strong>17.3.</strong> 面向对象设计模式的实现</a></li></ul></li></ul>
|
|
|
</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">
|
|
|
<a class="header" href="#处理环境变量" name="处理环境变量"><h2>处理环境变量</h2></a>
|
|
|
<blockquote>
|
|
|
<p><a href="https://github.com/rust-lang/book/blob/master/second-edition/src/ch12-05-working-with-environment-variables.md">ch12-05-working-with-environment-variables.md</a>
|
|
|
<br>
|
|
|
commit 0db6a0a34886bf02feabcab8b430b5d332a8bdf5</p>
|
|
|
</blockquote>
|
|
|
<p>我们将用一个额外的功能来改进我们的工具:一个通过环境变量启用的大小写不敏感搜索的选项。我们将其设计为一个命令行参数并要求用户每次需要时都加上它,不过相反我们将使用环境变量。这允许用户设置环境变量一次之后在整个终端会话中所有的搜索都将是大小写不敏感的了。</p>
|
|
|
<a class="header" href="#编写一个大小写不敏感search函数的失败测试" name="编写一个大小写不敏感search函数的失败测试"><h3>编写一个大小写不敏感<code>search</code>函数的失败测试</h3></a>
|
|
|
<p>首先,增加一个新函数,当设置了环境变量时会调用它。</p>
|
|
|
<!-- You mean, to turn the environment variable on? I'm not sure what we're
|
|
|
doing here-->
|
|
|
<!-- No, I'm not sure how this is unclear. We're adding a new function. We will
|
|
|
call the new function when the user turns on the environment variable. Can you
|
|
|
elaborate on what part of the above statement leads to the conclusion that the
|
|
|
new function is going to turn the environment variable on? Can you suggest a
|
|
|
rewording that makes the causality direction clearer? /Carol -->
|
|
|
<p>这里将继续遵循上一部分开始使用的 TDD 过程,其第一步是再次编写一个失败测试。我们将为新的大小写不敏感搜索函数新增一个测试函数,并将老的测试函数从<code>one_result</code>改名为<code>case_sensitive</code>来更清除的表明这两个测试的区别,如列表 12-20 所示:</p>
|
|
|
<p><span class="filename">Filename: src/lib.rs</span></p>
|
|
|
<pre><code class="language-rust">#[cfg(test)]
|
|
|
mod test {
|
|
|
use super::*;
|
|
|
|
|
|
#[test]
|
|
|
fn case_sensitive() {
|
|
|
let query = "duct";
|
|
|
let contents = "\
|
|
|
Rust:
|
|
|
safe, fast, productive.
|
|
|
Pick three.
|
|
|
Duct tape.";
|
|
|
|
|
|
assert_eq!(
|
|
|
vec!["safe, fast, productive."],
|
|
|
search(query, contents)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
|
fn case_insensitive() {
|
|
|
let query = "rUsT";
|
|
|
let contents = "\
|
|
|
Rust:
|
|
|
safe, fast, productive.
|
|
|
Pick three.
|
|
|
Trust me.";
|
|
|
|
|
|
assert_eq!(
|
|
|
vec!["Rust:", "Trust me."],
|
|
|
search_case_insensitive(query, contents)
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
|
|
|
<p><span class="caption">Listing 12-20: Adding a new failing test for the case
|
|
|
insensitive function we're about to add</span></p>
|
|
|
<p>注意我们也改变了老测试中<code>query</code>和<code>contents</code>的值:将查询字符串改变为 "duct",它将会匹配带有单词 productive" 的行。还新增了一个含有文本 "Duct tape" 的行,它有一个大写的 D,这在大小写敏感搜索时不应该匹配 "duct"。我们修改这个测试以确保不会意外破坏已经实现的大小写敏感搜索功能;这个测试现在应该能通过并在处理大小写不敏感搜索时应该能一直通过。</p>
|
|
|
<p>大小写不敏感搜索的新测试使用带有一些大写字母的 "rUsT" 作为其查询字符串。我们将要增加的<code>search_case_insensitive</code>的期望返回值是包含查询字符串 "rust" 的两行,"Rust:" 包含一个大写的 R 还有"Trust me."包含一个小写的 r。这个测试现在会编译失败因为还没有定义<code>search_case_insensitive</code>函数;请随意增加一个总是返回空 vector 的骨架实现,正如列表 12-16 中<code>search</code>函数那样为了使测试编译并失败时所做的那样。</p>
|
|
|
<a class="header" href="#实现search_case_insensitive函数" name="实现search_case_insensitive函数"><h3>实现<code>search_case_insensitive</code>函数</h3></a>
|
|
|
<p><code>search_case_insensitive</code>函数,如列表 12-21 所示,将与<code>search</code>函数基本相同。区别是它会将<code>query</code>变量和每一<code>line</code>都变为小写,这样不管输入参数是大写还是小写,在检查该行是否包含查询字符串时都会是小写。</p>
|
|
|
<p><span class="filename">Filename: src/lib.rs</span></p>
|
|
|
<pre><code class="language-rust">fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
|
|
let query = query.to_lowercase();
|
|
|
let mut results = Vec::new();
|
|
|
|
|
|
for line in contents.lines() {
|
|
|
if line.to_lowercase().contains(&query) {
|
|
|
results.push(line);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
results
|
|
|
}
|
|
|
</code></pre>
|
|
|
<p><span class="caption">Listing 12-21: Defining the <code>search_case_insensitive</code>
|
|
|
function to lowercase both the query and the line before comparing them</span></p>
|
|
|
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
|
|
|
<!-- why do we lowercase the search string? and why does it need to be a string
|
|
|
rather than a slice? -->
|
|
|
<!-- We explained this above, that in order to make the search case
|
|
|
insensitive, we need to lowercase everything so that searches will always match
|
|
|
no matter what case either the query or each line uses. It needs to be a
|
|
|
`String` because we're creating new data, not referencing existing data, when
|
|
|
we call `to_lowercase`. I've tried to make both of these points clearer, but
|
|
|
I'm not sure exactly what was unclear about it before, so I'm not sure if I've
|
|
|
helped. /Carol -->
|
|
|
<p>首先我们将<code>query</code>字符串转换为小写,并将其储存(覆盖)到同名的变量中。对查询字符串调用<code>to_lowercase</code>是必需的这样不管用户的查询是"rust"、"RUST"、"Rust"或者"rUsT",我们都将其当作"rust"处理并对大小写不敏感。</p>
|
|
|
<p>注意<code>query</code>现在是一个<code>String</code>而不是字符串 slice,因为调用<code>to_lowercase</code>是在创建新数据,而不是引用现有数据。如果查询字符串是"rUsT",这个字符串 slice 并不包含可供我们使用的小写的 u,所以必需分配一个包含"rust"的新<code>String</code>。因为<code>query</code>现在是一个<code>String</code>,当我们将<code>query</code>作为一个参数传递给<code>contains</code>方法时,需要增加一个 & 因为<code>contains</code>的签名被定义为获取一个字符串 slice。</p>
|
|
|
<p>接下来在检查每个<code>line</code>是否包含<code>search</code>之前增加了一个<code>to_lowercase</code>调用。这会将"Rust:"变为"rust:"并将"Trust me."变为"trust me."。现在我们将<code>line</code>和<code>query</code>都转换成了小写,这样就可以不管大小写的匹配文件中的文本和用户输入的查询了。</p>
|
|
|
<p>让我们看看这个实现能否通过测试:</p>
|
|
|
<pre><code> Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
|
|
|
Running target/debug/deps/greprs-e58e9b12d35dc861
|
|
|
|
|
|
running 2 tests
|
|
|
test test::case_insensitive ... ok
|
|
|
test test::case_sensitive ... ok
|
|
|
|
|
|
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
|
|
|
|
|
|
Running target/debug/greprs-8a7faa2662b5030a
|
|
|
|
|
|
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>run</code>函数中调用真正的新<code>search_case_insensitive</code>函数。首先,我们将在<code>Config</code>结构体中增加一个配置项来切换大小写敏感和大小写不敏感搜索。</p>
|
|
|
<p><span class="filename">Filename: src/lib.rs</span></p>
|
|
|
<pre><code class="language-rust">pub struct Config {
|
|
|
pub query: String,
|
|
|
pub filename: String,
|
|
|
pub case_sensitive: bool,
|
|
|
}
|
|
|
</code></pre>
|
|
|
<!-- Will add ghosting in libreoffice /Carol -->
|
|
|
<p>这里增加了<code>case_sensitive</code>字符来存放一个布尔值。接着我们需要<code>run</code>函数检查<code>case_sensitive</code>字段的值并使用它来决定是否调用<code>search</code>函数或<code>search_case_insensitive</code>函数,如列表 12-22所示:</p>
|
|
|
<p><span class="filename">Filename: src/lib.rs</span></p>
|
|
|
<pre><code class="language-rust"># use std::error::Error;
|
|
|
# use std::fs::File;
|
|
|
# use std::io::prelude::*;
|
|
|
#
|
|
|
# fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
|
|
# vec![]
|
|
|
# }
|
|
|
#
|
|
|
# fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
|
|
# vec![]
|
|
|
# }
|
|
|
#
|
|
|
# struct Config {
|
|
|
# query: String,
|
|
|
# filename: String,
|
|
|
# case_sensitive: bool,
|
|
|
# }
|
|
|
#
|
|
|
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)?;
|
|
|
|
|
|
let results = if config.case_sensitive {
|
|
|
search(&config.query, &contents)
|
|
|
} else {
|
|
|
search_case_insensitive(&config.query, &contents)
|
|
|
};
|
|
|
|
|
|
for line in results {
|
|
|
println!("{}", line);
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
}
|
|
|
</code></pre>
|
|
|
<p><span class="caption">Listing 12-22: Calling either <code>search</code> or
|
|
|
<code>search_case_insensitive</code> based on the value in <code>config.case_sensitive</code></span></p>
|
|
|
<!-- Will add ghosting in libreoffice /Carol -->
|
|
|
<p>最后需要实际检查环境变量。处理环境变量的函数位于标准库的<code>env</code>模块中,所以我们需要在 <em>src/lib.rs</em> 的开头增加一个<code>use std::env;</code>行将这个模块引入作用域中。接着在<code>Config::new</code>中使用<code>env</code>模块的<code>var</code>方法检查一个叫做<code>CASE_INSENSITIVE</code>的环境变量,如列表 12-23 所示:</p>
|
|
|
<p><span class="filename">Filename: src/lib.rs</span></p>
|
|
|
<pre><code class="language-rust">use std::env;
|
|
|
# struct Config {
|
|
|
# query: String,
|
|
|
# filename: String,
|
|
|
# case_sensitive: bool,
|
|
|
# }
|
|
|
|
|
|
// ...snip...
|
|
|
|
|
|
impl Config {
|
|
|
pub fn new(args: &[String]) -> Result<Config, &'static str> {
|
|
|
if args.len() < 3 {
|
|
|
return Err("not enough arguments");
|
|
|
}
|
|
|
|
|
|
let query = args[1].clone();
|
|
|
let filename = args[2].clone();
|
|
|
|
|
|
let case_sensitive = env::var("CASE_INSENSITIVE").is_err();
|
|
|
|
|
|
Ok(Config {
|
|
|
query: query,
|
|
|
filename: filename,
|
|
|
case_sensitive: case_sensitive,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
<p><span class="caption">Listing 12-23: Checking for an environment variable named
|
|
|
<code>CASE_INSENSITIVE</code></span></p>
|
|
|
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
|
|
|
<p>这里创建了一个新变量<code>case_sensitive</code>。为了设置它的值,需要调用<code>env::var</code>函数并传递我们需要寻找的环境变量名称,<code>CASE_INSENSITIVE</code>。<code>env::var</code>返回一个<code>Result</code>,它在环境变量被设置时返回包含其值的<code>Ok</code>成员,并在环境变量未被设置时返回<code>Err</code>成员。我们使用<code>Result</code>的<code>is_err</code>方法来检查其是否是一个 error(也就是环境变量未被设置的情况),这也就意味着我们<strong>需要</strong>进行一个大小写敏感搜索。如果<code>CASE_INSENSITIVE</code>环境变量被设置为任何值,<code>is_err</code>会返回 false 并将进行大小写不敏感搜索。我们并不关心环境变量所设置的值,只关心它是否被设置了,所以检查<code>is_err</code>而不是<code>unwrap</code>、<code>expect</code>或任何我们已经见过的<code>Result</code>的方法。我们将变量<code>case_sensitive</code>的值传递给<code>Config</code>实例这样<code>run</code>函数可以读取其值并决定是否调用<code>search</code>或者列表 12-22 中实现的<code>search_case_insensitive</code>。</p>
|
|
|
<p>让我们试一试吧!首先不设置环境变量并使用查询"to"运行程序,这应该会匹配任何全小写的单词"to"的行:</p>
|
|
|
<pre><code class="language-text">$ cargo run to poem.txt
|
|
|
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
|
|
|
Running `target/debug/greprs to poem.txt`
|
|
|
Are you nobody, too?
|
|
|
How dreary to be somebody!
|
|
|
</code></pre>
|
|
|
<p>看起来程序仍然能够工作!现在将<code>CASE_INSENSITIVE</code>设置为 1 并仍使用相同的查询"to",这回应该得到包含可能有大写字母的"to"的行:</p>
|
|
|
<pre><code>$ CASE_INSENSITIVE=1 cargo run to poem.txt
|
|
|
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
|
|
|
Running `target/debug/greprs to poem.txt`
|
|
|
Are you nobody, too?
|
|
|
How dreary to be somebody!
|
|
|
To tell your name the livelong day
|
|
|
To an admiring bog!
|
|
|
</code></pre>
|
|
|
<p>好极了,我们也得到了包含"To"的行!现在<code>greprs</code>程序可以通过环境变量控制进行大小写不敏感搜索了。现在你知道了如何管理由命令行参数或环境变量设置的选项了!</p>
|
|
|
<p>一些程序允许对相同配置同时使用参数<strong>和</strong>环境变量。在这种情况下,程序来决定参数和环境变量的优先级。作为一个留给你的测试,尝试同时通过一个命令行参数来控制大小写不敏感搜索,并在程序遇到矛盾值时决定其优先级。</p>
|
|
|
<p><code>std::env</code>模块还包含了更多处理环境变量的实用功能;请查看官方文档来了解其可用的功能。</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
|
|
<a href="ch12-04-testing-the-librarys-functionality.html" class="mobile-nav-chapters previous">
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a href="ch12-06-writing-to-stderr-instead-of-stdout.html" class="mobile-nav-chapters next">
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
<a href="ch12-04-testing-the-librarys-functionality.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-06-writing-to-stderr-instead-of-stdout.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>
|