In the following code, how can I return the reference of floor
instead of a new object? Is it possible to let the function return either a borrowed reference or an owned value?
extern crate num; // 0.2.0
use num::bigint::BigInt;
fn cal(a: BigInt, b: BigInt, floor: &BigInt) -> BigInt {
let c: BigInt = a - b;
if c.ge(floor) {
c
} else {
floor.clone()
}
}
Since
BigInt
implementsClone
, you can use astd::borrow::Cow
:Later, you can use
Cow::into_owned()
to get a owned version ofBigInt
, or just use it as a reference: