知识卡片

普通读书笔记卡 · 6074.a.3 · 生成模型:未记录

解析由目标类型决定

理解等级:中级

同一段文本可尝试解析成多种类型。parse 需要调用位置给出目标,let n: u32 可提供约束。终端文本往往含换行,先 trim 再解析。负数、越界值和非法字符等情况仍须按目标类型处理返回的错误。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 猜数字游戏 Comparing the Guess to the Secret Number · 查看原书依据

      The parse method on strings converts a string to another type. Here, we use it to convert from a string to a number. We need to tell Rust the exact number type we want by using let guess: u32. The colon (:) after guess tells Rust we’ll annotate the variable’s type. Rust has a few built-in number types; the u32 seen here is an unsigned, 32-bit integer.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第02章 猜数字游戏(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c02.xhtml`,块 0136) - 结论依据:书中以 let guess: u32 提供 parse 所需的具体类型。 - 原始内容:The parse method on strings converts a string to another type. Here, we use it to convert from a string to a number. We need to tell Rust the exact number type we want by using let guess: u32. The colon (:) after guess tells Rust we’ll annotate the variable’s type. Rust has a few built-in number types; the u32 seen here is an unsigned, 32-bit integer.