std-method
Integer checked_shl and checked_shr
Validate a bit position before shifting. A valid left shift can discard high bits; checked_shl does not verify mathematical multiplication. Expected stdout (not executed): Some(0) None Some(8) Common mistake: Assuming Some implies no bits were lost.
Formal Syntax & Structure
rust syntaxrust
x.checked_shl(bits: u32); x.checked_shr(bits: u32)Working Example
Example Snippetrust
fn main() { println!("{:?}", 128u8.checked_shl(1)); println!("{:?}", 1u8.checked_shl(8)); println!("{:?}", 64u8.checked_shr(3));}