我的印象是整数类型的 .checked_*(_)
方法可以帮助避免溢出。然而,.checked_shl(u32)
方法愉快地移出了上面示例的最后一位。
我的印象有误吗?该方法用于什么?
(还想补充一点以避免轮类溢出,至少对于无符号类型,可以检查是否 ((~0) >> rhs) >= self
)
最佳答案
因为它只检查偏移量。来自docs ,
None
ifrhs
is larger than or equal to the number of bits inself
.
所以根据设计,它允许您移出位,它只是不允许您使用无效的移位量(或者,它允许您,但您得到 None
)。
关于rust - 为什么 `128u8.checked_shl(1)` 返回 `Some(0)` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37860831/