Rust 编程:从基础程序到命令行参数处理
1. 运行本地二进制程序
在开发 Rust 程序时,有时需要运行仅存在于当前 crate 中的二进制文件。例如,当我们进入 target/debug/ 目录尝试运行 hello 程序时,可能会遇到如下情况:
$ cd target/debug/
$ hello
-bash: hello: command not found
这表明我们需要显式引用当前工作目录才能运行该程序:
$ ./hello
Hello, world!
1.1 添加项目依赖
为了更方便地测试当前 crate 中的程序,我们可以使用 assert_cmd crate。首先,需要将其作为开发依赖添加到 Cargo.toml 中:
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
[dependencies]
[dev-dependencies]
assert_cmd = "1"
然后,更新 tests/cli.rs 中的 runs 函数,使用 assert_cmd::Command
超级会员免费看
订阅专栏 解锁全文
2558

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



