相关疑难解决方法(0)

为什么不打印!在Rust单元测试中工作?

我已经实现了以下方法和单元测试:

use std::fs::File;
use std::path::Path;
use std::io::prelude::*;

fn read_file(path: &Path) {
    let mut file = File::open(path).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    println!("{}", contents);
}

#[test]
fn test_read_file() {
    let path = &Path::new("/etc/hosts");
    println!("{:?}", path);
    read_file(path);
}
Run Code Online (Sandbox Code Playgroud)

我以这种方式运行单元测试:

rustc --test app.rs; ./app
Run Code Online (Sandbox Code Playgroud)

我也可以运行它

cargo test
Run Code Online (Sandbox Code Playgroud)

我收到一条消息说测试已通过,但println!屏幕上从未显示过.为什么不?

println rust

237
推荐指数
7
解决办法
4万
查看次数

标签 统计

println ×1

rust ×1