Rust错误分析

Summary: Author: 张亚飞 | 阅读时间: 1 minute read | Published: 2019-11-19
Filed under Categories: RustTags: Rust,

Rust开发调试工具及错误分析

定位文件路径

使用 cargo 安装 Rust 项目后运行报错:

# cargo install --path=./ --force
# run_s tcs test -i 0000000000000
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" })', src/libcore/result.rs:1187:5
stack backtrace:

如果尝试打开一个不存在的问题将会报错:

std::fs::read_to_string("/test.txt")
//Os { code: 2, kind: NotFound, message: "No such file or directory" }'

如何像下面这样打印具体出错的文件名:

Files::read_to_string(path)
//Os { code: 2, kind: NotFound, message: "No such file or directory" , name: "/test.txt"}'

如果有以下功能

fn main() {
    // outputs "Filesystem("/test.txt", "reading", Os { code: 2, kind: NotFound, message: "No such file or directory" })"
    ex::fs::read_to_string("/test.txt").unwrap(); 
}

无法定位问题,使用 ex

fn main() {
    let res = fallible_main();
    if let Err(e) = res {
        eprintln!("Error occurred: {}", e);
    }
}

fn fallible_main() -> Result<(), ex::io::Error> {
    ex::fs::read_to_string("/test.txt")?;
    
    Ok(())
}

将会输出以下错误:

Error occurred: error reading "/test.txt": No such file or directory (os error 2)

Get which filename is not found on rust file opening.


Comments

Cor-Ethan, the beverage → www.iirii.com