读取文件
现在我们将添加读取 file_path
参数中指定文件的功能。首先我们需要一个示例文件来测试它:我们将使用一个包含少量文本的文件,文本分布在多行上,并包含一些重复的单词。列表 12-3 包含了一首艾米莉·狄金森的诗,它会很好用!在你的项目的根级别创建一个名为 poem.txt 的文件,并输入诗歌 “I’m Nobody! Who are you?”
I'm nobody! Who are you?
Are you nobody, too?
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!
将文本放入到位后,编辑 src/main.rs 并添加代码以读取文件,如列表 12-4 所示。
use std::env;
use std::fs;
fn main() {
// --snip--
let args: Vec<String> = env::args().collect();
let query = &args[1];
let file_path = &args[2];
println!("Searching for {query}");
println!("In file {file_path}");
let contents = fs::read_to_string(file_path)
.expect("Should have been able to read the file");
println!("With text:\n{contents}");
}
首先,我们使用 use
语句引入标准库的相关部分:我们需要 std::fs
来处理文件。
在 main
中,新的语句 fs::read_to_string
接收 file_path
,打开该文件,并返回类型为 std::io::Result<String>
的值,其中包含文件的内容。
之后,我们再次添加一个临时的 println!
语句,该语句在读取文件后打印 contents
的值,以便我们可以检查程序到目前为止是否正常工作。
让我们使用任意字符串作为第一个命令行参数(因为我们尚未实现搜索部分)和 poem.txt 文件作为第二个参数来运行此代码
$ cargo run -- the poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep the poem.txt`
Searching for the
In file poem.txt
With text:
I'm nobody! Who are you?
Are you nobody, too?
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!
太棒了!代码读取然后打印了文件的内容。但是该代码有一些缺陷。目前,main
函数承担了多项职责:通常,如果每个函数仅负责一个想法,则函数会更清晰且更易于维护。另一个问题是,我们没有像我们本可以做到的那样处理错误。程序仍然很小,因此这些缺陷不是大问题,但是随着程序的增长,将更难干净地修复它们。在开发程序时尽早开始重构是一个好习惯,因为重构少量代码要容易得多。我们将在下一步进行重构。