Hi, I rarely use wrapping_mul in my code, but something looks fishy on the following snippet:
pub fn test() {
let vec: Vec<u64> = vec![
23, 24, 25, 29, 34, 36, 38, 41, 42, 45, 46, 47, 49, 51, 56, 58, 60, 61, 62, 63, 65, 69, 70,
72, 74, 78, 79, 82, 84, 85, 86, 87, 89, 90, 94, 95, 99, 101, 102, 103, 104, 106, 112, 113,
114, 117, 118, 122, 123, 124, 125, 126, 128, 129, 131, 132, 133, 136, 141, 147, 149, 154,
155, 163, 167, 169, 171, 174, 175, 176, 177, 178, 180, 182, 188, 195, 198, 205, 206, 208,
209, 210, 214, 215, 216, 217, 219, 220, 221, 222, 223, 224, 225, 230, 245, 246, 255, 256,
258, 262, 273, 276, 277, 278, 279, 280, 284, 293, 294, 296, 298, 300, 301, 302, 310, 311,
313, 315, 324, 325, 328, 330, 331, 333, 335, 336, 340, 341, 342, 345, 346, 347, 348, 350,
351, 354, 356, 357, 358, 363, 366, 367, 368, 369, 370, 373,
];
let mut sum = 1_u64;
for val in vec {
if val != 0 {
let tmp = sum.wrapping_mul(val);
if tmp != 0 {
sum = tmp;
}
}
}
println!("Sum {}", sum);
}
I yields 9223372036854775808, and seems to lock before value 180 is reached with single bits values. Why is that? It looks like saturating instead of wrapping at some points. Makes the same thing with prior versions of rust. So it's my understanding failing me.
Thanks!