如何在 Rust 中将 u32 数据类型转换为 bigInt?

Nat*_*pat 0 type-conversion fibonacci rust

我正在做斐波那契序列函数,返回类型应该返回 BigUint 类型。我搜索了很多网站,但找不到指明方向的网站。这是我到目前为止的过程。

pub fn fibo_seq(n: u32) -> Vec<num_bigint::BigUint> {
    let mut f1 = 0;
    let mut temp = 1;
    let mut f2 = 1;
    let mut vec:Vec<u32> = Vec::new();

    while vec.len() < n.try_into().unwrap(){
       vec.push(f2);
       temp = f2;
       f2 = f1 + f2;
       f1 = temp;  

    }

    vec // return wrong data type

}
Run Code Online (Sandbox Code Playgroud)

Pet*_*all 6

您不能只输入BigUint返回类型,您必须实际使用它!

pub fn fibo_seq(n: u32) -> Vec<BigUint> {
    let mut f1 = BigUint::from(0u32);
    let mut temp = BigUint::from(1u32);
    let mut f2 = BigUint::from(1u32);
    let mut vec: Vec<BigUint> = Vec::new();

    while vec.len() < n.try_into().unwrap(){
       vec.push(f2.clone());
       temp = f2.clone();
       f2 = f1 + &f2;
       f1 = temp;  

    }

    vec
}
Run Code Online (Sandbox Code Playgroud)