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-05-31 23:48:57 +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-05-working-with-environment-variables.md" > ch12-05-working-with-environment-variables.md< / a >
< br >
2017-04-18 14:55:09 +08:00
commit 0db6a0a34886bf02feabcab8b430b5d332a8bdf5< / p >
2017-03-06 22:56:55 +08:00
< / blockquote >
2017-04-24 23:03:13 +08:00
< p > 我们将用一个额外的功能来改进我们的工具:一个通过环境变量启用的大小写不敏感搜索的选项。我们将其设计为一个命令行参数并要求用户每次需要时都加上它,不过相反我们将使用环境变量。这允许用户设置环境变量一次之后在整个终端会话中所有的搜索都将是大小写不敏感的了。< / p >
2017-04-18 14:55:09 +08:00
< 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)]
2017-03-06 22:56:55 +08:00
mod test {
2017-04-18 14:55:09 +08:00
use super::*;
2017-03-06 22:56:55 +08:00
#[test]
fn case_sensitive() {
2017-04-18 14:55:09 +08:00
let query = " duct" ;
2017-03-06 22:56:55 +08:00
let contents = " \
Rust:
safe, fast, productive.
Pick three.
Duct tape." ;
assert_eq!(
vec![" safe, fast, productive." ],
2017-04-18 14:55:09 +08:00
search(query, contents)
2017-03-06 22:56:55 +08:00
);
}
#[test]
fn case_insensitive() {
2017-04-18 14:55:09 +08:00
let query = " rUsT" ;
2017-03-06 22:56:55 +08:00
let contents = " \
Rust:
safe, fast, productive.
Pick three.
Trust me." ;
assert_eq!(
vec![" Rust:" , " Trust me." ],
2017-04-18 14:55:09 +08:00
search_case_insensitive(query, contents)
2017-03-06 22:56:55 +08:00
);
}
}
< / code > < / pre >
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
2017-04-18 14:55:09 +08:00
< 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 >
2017-03-27 16:17:20 +08:00
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
2017-04-18 14:55:09 +08:00
< pre > < code class = "language-rust" > fn search_case_insensitive< 'a> (query: & str, contents: & 'a str) -> Vec< & 'a str> {
let query = query.to_lowercase();
2017-03-06 22:56:55 +08:00
let mut results = Vec::new();
for line in contents.lines() {
2017-04-18 14:55:09 +08:00
if line.to_lowercase().contains(& query) {
2017-03-06 22:56:55 +08:00
results.push(line);
}
}
results
}
< / code > < / pre >
2017-04-18 14:55:09 +08:00
< 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 >
2017-03-06 22:56:55 +08:00
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
2017-04-18 14:55:09 +08:00
<!-- 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 >
2017-03-07 23:28:30 +08:00
< pre > < code > Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
2017-04-18 14:55:09 +08:00
Running target/debug/deps/greprs-e58e9b12d35dc861
2017-03-07 23:28:30 +08:00
running 2 tests
test test::case_insensitive ... ok
test test::case_sensitive ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
2017-04-18 14:55:09 +08:00
Running target/debug/greprs-8a7faa2662b5030a
2017-03-07 23:28:30 +08:00
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 >
2017-04-18 14:55:09 +08:00
< p > 好的!现在,让我们在< code > run< / code > 函数中调用真正的新< code > search_case_insensitive< / code > 函数。首先,我们将在< code > Config< / code > 结构体中增加一个配置项来切换大小写敏感和大小写不敏感搜索。< / p >
2017-03-07 23:28:30 +08:00
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
< pre > < code class = "language-rust" > pub struct Config {
2017-04-18 14:55:09 +08:00
pub query: String,
2017-03-07 23:28:30 +08:00
pub filename: String,
pub case_sensitive: bool,
}
< / code > < / pre >
<!-- Will add ghosting in libreoffice /Carol -->
2017-04-18 14:55:09 +08:00
< p > 这里增加了< code > case_sensitive< / code > 字符来存放一个布尔值。接着我们需要< code > run< / code > 函数检查< code > case_sensitive< / code > 字段的值并使用它来决定是否调用< code > search< / code > 函数或< code > search_case_insensitive< / code > 函数,如列表 12-22所示: < / p >
2017-03-07 23:28:30 +08:00
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
2017-04-18 14:55:09 +08:00
< 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> > {
2017-03-07 23:28:30 +08:00
let mut f = File::open(config.filename)?;
let mut contents = String::new();
f.read_to_string(& mut contents)?;
let results = if config.case_sensitive {
2017-04-18 14:55:09 +08:00
search(& config.query, & contents)
2017-03-07 23:28:30 +08:00
} else {
2017-04-18 14:55:09 +08:00
search_case_insensitive(& config.query, & contents)
2017-03-07 23:28:30 +08:00
};
for line in results {
println!(" {}" , line);
}
Ok(())
}
< / code > < / pre >
2017-04-18 14:55:09 +08:00
< 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 >
2017-03-07 23:28:30 +08:00
<!-- Will add ghosting in libreoffice /Carol -->
2017-04-18 14:55:09 +08:00
< 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 >
2017-03-07 23:28:30 +08:00
< p > < span class = "filename" > Filename: src/lib.rs< / span > < / p >
< pre > < code class = "language-rust" > use std::env;
# struct Config {
2017-04-18 14:55:09 +08:00
# query: String,
2017-03-07 23:28:30 +08:00
# filename: String,
# case_sensitive: bool,
# }
2017-04-18 14:55:09 +08:00
// ...snip...
2017-03-07 23:28:30 +08:00
impl Config {
pub fn new(args: & [String]) -> Result< Config, & 'static str> {
if args.len() < 3 {
return Err(" not enough arguments" );
}
2017-04-18 14:55:09 +08:00
let query = args[1].clone();
2017-03-07 23:28:30 +08:00
let filename = args[2].clone();
2017-04-18 14:55:09 +08:00
let case_sensitive = env::var(" CASE_INSENSITIVE" ).is_err();
2017-03-07 23:28:30 +08:00
Ok(Config {
2017-04-18 14:55:09 +08:00
query: query,
2017-03-07 23:28:30 +08:00
filename: filename,
case_sensitive: case_sensitive,
})
}
}
< / code > < / pre >
2017-04-18 14:55:09 +08:00
< p > < span class = "caption" > Listing 12-23: Checking for an environment variable named
< code > CASE_INSENSITIVE< / code > < / span > < / p >
2017-03-07 23:28:30 +08:00
<!-- Will add ghosting and wingdings in libreoffice /Carol -->
2017-04-18 14:55:09 +08:00
< 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
2017-03-07 23:28:30 +08:00
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
2017-04-18 14:55:09 +08:00
Running `target/debug/greprs to poem.txt`
2017-03-07 23:28:30 +08:00
Are you nobody, too?
How dreary to be somebody!
< / code > < / pre >
2017-04-18 14:55:09 +08:00
< p > 看起来程序仍然能够工作!现在将< code > CASE_INSENSITIVE< / code > 设置为 1 并仍使用相同的查询" to" ,这回应该得到包含可能有大写字母的" to" 的行:< / p >
2017-03-07 23:28:30 +08:00
< pre > < code > $ CASE_INSENSITIVE=1 cargo run to poem.txt
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
2017-04-18 14:55:09 +08:00
Running `target/debug/greprs to poem.txt`
2017-03-07 23:28:30 +08:00
Are you nobody, too?
How dreary to be somebody!
To tell your name the livelong day
To an admiring bog!
< / code > < / pre >
2017-04-18 14:55:09 +08:00
< p > 好极了,我们也得到了包含" To" 的行!现在< code > greprs< / code > 程序可以通过环境变量控制进行大小写不敏感搜索了。现在你知道了如何管理由命令行参数或环境变量设置的选项了!< / p >
< p > 一些程序允许对相同配置同时使用参数< strong > 和< / strong > 环境变量。在这种情况下,程序来决定参数和环境变量的优先级。作为一个留给你的测试,尝试同时通过一个命令行参数来控制大小写不敏感搜索,并在程序遇到矛盾值时决定其优先级。< / p >
2017-03-07 23:28:30 +08:00
< p > < code > std::env< / code > 模块还包含了更多处理环境变量的实用功能;请查看官方文档来了解其可用的功能。< / p >
2017-03-06 22:56:55 +08:00
2017-03-02 23:36:03 +08:00
< / 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 >