知识卡片
从命令行读取参数
调用 env::args() 得到参数迭代器。用 let args: Vec<String> = env::args().collect() 指定收集类型。Cargo 运行命令中用 -- 隔开程序实参。示例按索引取值,正式解析时先检查长度。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- I/O 项目:构建命令行程序 Accepting Command Line Arguments · 查看原书依据
Notice that the first value in the vector is "target/debug/minigrep", which is the name of our binary. This matches the behavior of the arguments list in C, letting programs use the name by which they were invoked in their execution.
参考来源
- 位置:《The Rust Programming Language(第2版)》第12章 I/O 项目:构建命令行程序(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c12.xhtml`,块 0032)
- 结论依据:调试输出显示调用名占第 0 项,查询和文件路径随后出现。
- 原始内容:Notice that the first value in the vector is "target/debug/minigrep", which is the name of our binary. This matches the behavior of the arguments list in C, letting programs use the name by which they were invoked in their execution.