Since BigInt
implements Clone
, you can use a std::borrow::Cow
:
use num::bigint::BigInt; // 0.2.0
use std::borrow::Cow;
fn cal(a: BigInt, b: BigInt, floor: &BigInt) -> Cow<BigInt> {
let c: BigInt = a - b;
if c.ge(floor) {
Cow::Owned(c)
} else {
Cow::Borrowed(floor)
}
}
Later, you can use Cow::into_owned()
to get a owned version of BigInt
, or just use it as a reference:
fn main() {
let a = BigInt::from(1);
let b = BigInt::from(2);
let c = &BigInt::from(3);
let result = cal(a, b, c);
let ref_result = &result;
println!("ref result: {}", ref_result);
let owned_result = result.into_owned();
println!("owned result: {}", owned_result);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…