Is it possible to return either a borrowed or owned type in Rust?

后端 未结 1 1765
轮回少年
轮回少年 2020-11-30 12:47

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

相关标签:
1条回答
  • 2020-11-30 13:20

    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);
    }
    
    0 讨论(0)
提交回复
热议问题