Kevin
Kevin

Reputation: 3368

Rust for loop with BigUint

I am new to rust and trying to write a for loop with BigUint:

use num_bigint::BigUint;

fn loop(k: &[u8]) -> BigUint {
    let mut k = BigUint::from_bytes_le(k);

    while k != BigUint::zero() {
        k >>= 1;
    }

    k
}

But when I try to compile i get the error:

no function or associated item named `zero` found for struct `num_bigint::BigUint` in the current scope
function or associated item not found in `num_bigint::BigUint`
help: items from traits can only be used if the trait is in scoperustc(E0599)
lib.rs(53, 25): function or associated item not found in `num_bigint::BigUint`

My Cargo.toml file has the following dependency:

[dependencies]
num-bigint = { version = "0.2.2" }

Am I missing some import? How can I use BigUint::zero() ?

Upvotes: 1

Views: 376

Answers (1)

eggyal
eggyal

Reputation: 125955

As the error suggests, "items from traits can only be used if the trait is in scope". The zero method is defined by the num_traits::Zero trait. Therefore, import it (you will need to add the num-traits dependency to your Cargo.toml too):

[dependencies]
num-bigint = "0.2.2"
num-traits = "0.2.14"
use num_traits::Zero;

Upvotes: 2

Related Questions