gpt4 book ai didi

rust - 使用 writeln 时必须使用的未使用的 `std::result::Result`

转载 作者:行者123 更新时间:2023-12-03 11:41:08 27 4
gpt4 key购买 nike

我正在做来自 https://rust-cli.github.io/book/tutorial/testing.html 的 rust cli 教程并遇到编译器警告:

unused `std::result::Result` that must be used
--> src/main.rs:15:13
|
15 | writeln!(writer, "{}", line);

完整代码如下:

use exitfailure::ExitFailure;
use failure::ResultExt;
use structopt::StructOpt;

#[derive(StructOpt)]
struct Cli {
pattern: String,
#[structopt(parse(from_os_str))]
path: std::path::PathBuf,
}

fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) {
for line in content.lines() {
if line.contains(pattern) {
writeln!(writer, "{}", line);
}
}
}

#[test]
fn find_a_match() {
let mut result = Vec::new();
find_matches("lorem ipsum\ndolor sit amet", "lorem", &mut result);
assert_eq!(result, b"lorem ipsum\n");
}

fn main() -> Result<(), ExitFailure> {
let args = Cli::from_args();
let content = std::fs::read_to_string(&args.path)
.with_context(|_| format!("could not read file `{}`", &args.path.display()))?;
find_matches(&content, &args.pattern, &mut std::io::stdout());
Ok(())
}

为什么我会收到此警告?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com