Pretty new to rust and am wondering if the following is a bug or something I am doing wrong:
I am trying to learn rust by doing project euler. I am at problem 14 which includes collatz sequence.I am using the colltaz crate but run into the following:
the last one crashes..
thread 'main' panicked at 'attempt to multiply with overflow', /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/ops/arith.rs:369:1
Probably the numbers you're calculating are getting too large for your integer type. Are you using u32 or maybe usize? Because u32 won't be big enough. Try u64 instead.
the number of steps should be around 524.. the intermediate numbers might be huge, but I cannot control those, they are processed inside the crate..
Thanks
I'm using collatz = "0.5.0". I have done all those in Freepascal and wrote my own function, but since the crate existed in Rust I decided to use it.. I might write my own function again, it is good practice.
The compiler has to eventually figure out the type of your literal (u32, i128, ...). Before it figures that out, it just flags the literal as "some kind of (built-in) {integer}". In this case, all of the built-in integer types meet the bounds of total_stopping_time. In situations like these, Rust falls back to a default integer type: i32. (The default float type is f64.)
So that's what's going on -- you're using i32s and they're overflowing on the intermediate values.
And thus the actual solution to your OP is to be explicit about your literal types:
Playground. Sorry for not understanding your actual question initially.
(Incidentally, this crate seems fine, but anyone can publish a crate -- so you can't really tell if one is any good (or familiar to everyone) just because it's on crates.io.)
Awesome
Thanks for your help. I need to define the variable not just for the value I give to the function, but also for the value it might get inside the function..
Marking the argument as u64 is just to aid type deduction; there is no “variable” defined here. You could also explicitly provide the type parameter and use