s and t are vectors. If we do this:
t.len() - s.len()
It may result in attempt to subtract with overflow error if the subtraction result is negative (since both types' are usize).
However, it may cause a tricky bug if we are not aware that both types are usize and we expect the result can be a negative number (I encountered myself and only catch it during runtime on edge cases). To fix the bug, I converted them to:
t.len() as i32 - s.len() as i32
My questions are:
- Why Rust doesn't convert
usizesubstraction to a signed type? - What's Rust idiomatic way to catching this type of bugs early during compile time?