num-bigint
num-bigint copied to clipboard
running example fail
Hi there, Just trying to run the example but there seems to be a problem to access RandBigInt. Any help?
main.rs
1 extern crate rand;
2 extern crate num_bigint as bigint;
3
4 use bigint::{ToBigInt, RandBigInt};
5
6 fn main() {
7 let mut rng = rand::thread_rng();
8 let a = rng.gen_bigint(1000);
9
10 let low = -10000.to_bigint().unwrap();
11 let high = 10000.to_bigint().unwrap();
12 let b = rng.gen_bigint_range(&low, &high);
13
14 // Probably an even larger number.
15 println!("{}", a * b);
16 }
Cargo.toml
6 [dependencies]
7 rand = "0.5"
8 num-bigint = "0.2"
...
Updating registry `https://github.com/rust-lang/crates.io-index`
Compiling num-traits v0.2.5
Compiling num-integer v0.1.39
Compiling num-bigint v0.2.0
Compiling libc v0.2.42
Compiling rand_core v0.2.1
Compiling rand v0.5.4
Compiling biginttesting v0.1.0
error[E0432]: unresolved import `bigint::RandBigInt`=============> ] 12/13: biginttesting
--> src/main.rs:4:24
|
4 | use bigint::{ToBigInt, RandBigInt};
| ^^^^^^^^^^ no `RandBigInt` in the root
warning: unused import: `RandBigInt`
--> src/main.rs:4:24
|
4 | use bigint::{ToBigInt, RandBigInt};
| ^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error[E0599]: no method named `gen_bigint` found for type `rand::ThreadRng` in the current scope
--> src/main.rs:8:17
|
8 | let a = rng.gen_bigint(1000);
| ^^^^^^^^^^
error[E0599]: no method named `gen_bigint_range` found for type `rand::ThreadRng` in the current scope
--> src/main.rs:12:17
|
12 | let b = rng.gen_bigint_range(&low, &high);
| ^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
Ah, the rand dependency is no longer enabled in the crate by default -- independent of you having rand yourself. You'll have to enable it like:
[dependencies.num-bigint]
version = "0.2"
features = ["rand"]
We should clarify this in the example description. (I think the code could be simplified in a few ways too.)
Thanks :-)
This should probably be changed in the features section of the documentation as well.
I was thoroughly confused for a few moments when I wasn't able to use RandBigInt and it seemed as if there weren't any features to enable
Fails when compiled with newer rand = "0.7.0" on the same example code.
error[E0599]: no method named `gen_bigint` found for type `rand::prelude::ThreadRng` in the current scope
--> src/main.rs:8:17
|
8 | let a = rng.gen_bigint(1000);
| ^^^^^^^^^^
|
= note: the method `gen_bigint` exists but the following trait bounds were not satisfied:
`rand::prelude::ThreadRng : num_bigint::RandBigInt`
error[E0599]: no method named `gen_bigint_range` found for type `rand::prelude::ThreadRng` in the current scope
--> src/main.rs:12:17
|
12 | let b = rng.gen_bigint_range(&low, &high);
| ^^^^^^^^^^^^^^^^
|
= note: the method `gen_bigint_range` exists but the following trait bounds were not satisfied:
`rand::prelude::ThreadRng : num_bigint::RandBigInt`
warning: unused import: `RandBigInt`
--> src/main.rs:4:18
|
4 | use num_bigint::{RandBigInt, ToBigInt};
| ^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error: aborting due to 2 previous errors
Fails when compiled with newer rand = "0.7.0" on the same example code. error[E0599]: no method named
gen_bigintfound for typerand::prelude::ThreadRngin the current scope --> src/main.rs:8:17 | 8 | let a = rng.gen_bigint(1000); | ^^^^^^^^^^ | = note: the methodgen_bigintexists but the following trait bounds were not satisfied:rand::prelude::ThreadRng : num_bigint::RandBigInterror[E0599]: no method named
gen_bigint_rangefound for typerand::prelude::ThreadRngin the current scope --> src/main.rs:12:17 | 12 | let b = rng.gen_bigint_range(&low, &high); | ^^^^^^^^^^^^^^^^ | = note: the methodgen_bigint_rangeexists but the following trait bounds were not satisfied:rand::prelude::ThreadRng : num_bigint::RandBigIntwarning: unused import:
RandBigInt--> src/main.rs:4:18 | 4 | use num_bigint::{RandBigInt, ToBigInt}; | ^^^^^^^^^^ | = note: #[warn(unused_imports)] on by defaulterror: aborting due to 2 previous errors
The last comment from MitchellB in this post https://stackoverflow.com/questions/51640381/how-do-i-generate-a-random-numbiguint helped me. But yes, it doesn't work with the newer version of rand (0.7.0).
#75