← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 10640.b · 生成模型:未记录

从命令行读取参数

理解等级:初级

调用 env::args() 得到参数迭代器。用 let args: Vec<String> = env::args().collect() 指定收集类型。Cargo 运行命令中用 -- 隔开程序实参。示例按索引取值,正式解析时先检查长度。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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.