Rust 编程:实现类似 cat 和 head 功能的程序
1. 实现类似 cat 功能程序的初步探索
1.1 测试用例与标准输入处理
#[test]
fn bustle_stdin() -> TestResult {
run_stdin(BUSTLE, &["-"], "tests/expected/the-bustle.txt.stdin.out")
}
此测试用例的作用是,将指定文件名的内容作为标准输入,以破折号作为输入文件名来运行程序,并验证输出是否与预期值相符。
1.2 读取文件行内容
pub fn run(config: Config) -> MyResult<()> {
for filename in config.files {
match open(&filename) {
Err(err) => eprintln!("{}: {}", filename, err),
Ok(file) => {
for line_result in file.lines() {
let line = line_result?;
println!("{}", line);
}
}
超级会员免费看
订阅专栏 解锁全文
869

被折叠的 条评论
为什么被折叠?



