← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 743.d.2.b.7.a · 生成模型:未记录

从输入边界区分错误

理解等级:中级

读入一行与把一行解释成数是两层操作。书中的游戏对读行失败使用 expect,对解析失败用 Err(_) => continue 保留交互。部署类似交互时,还应按读取 API 自行给 EOF 设置退出语义,避免把输入结束持续当成无效值。

相关卡片 [[解析由目标类型决定]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第02章 猜数字游戏(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c02.xhtml`,块 0163) - 结论依据:原文用 Err(_) 分支跳过无法解析的猜测并继续循环。 - 原始内容:If parse is not able to turn the string into a number, it will return an Err value that contains more information about the error. The Err value does not match the Ok(num) pattern in the first match arm, but it does match the Err(_) pattern in the second arm.