I'm trying to write a gcd function using BigUInt
from create num-bigint.
fn gcd<'a>(mut a: &'a BigUint, mut b: &'a BigUint) -> &'a BigUint {
while *b != Zero::zero() {
let t = b;
b = &a.modpow(&One::one(), b);
a = t;
}
return a;
}
but I get a compilation error:
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:63:18
|
60 | fn gcd<'a>(mut a: &'a BigUint, mut b: &'a BigUint) -> &'a BigUint {
| -- lifetime `'a` defined here
...
63 | b = &a.modpow(&One::one(), b);
| -----^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| assignment requires that borrow lasts for `'a`
How to fix this? I don't want to clone the BigUInt
repeatedly since those are supposed to be big.
Note: Zero::zero()
and One::one()
are from num-traits create.