知识卡片
整数写法与方法消歧
整数可加 u8 等后缀,写成 127_u8;0x、0o、0b 指定进制,下划线便于阅读。缺少后缀时根据使用场景推断,最后回落到 i32。若 (-4).abs() 在方法解析时还不确定是哪种整数,最后的回落来不及帮助解析。可写 (-4_i32).abs() 或 i32::abs(-4)。方法调用优先于前缀负号,因此 -4_i32.abs() 的结果是 −4。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Fundamental Types 整数的范围、写法与方法解析 · 查看原书依据
For technical reasons, Rust wants to know exactly which integer type a value has before it will call the type’s methods.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第02章《Fundamental Types》|整数的范围、写法与方法解析,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch02.xhtml#integer-types`
- 结论依据:原文给出方法解析报错、后缀和关联函数两种解法,并解释括号的作用。
- 原始内容:For technical reasons, Rust wants to know exactly which integer type a value has before it will call the type’s methods.