Example code fails to build?
jodavaho opened this issue · comments
I've been using statrs for some poisson testing, and it's fine.
https://github.com/jodavaho/poisson-rate-test/blob/main/src/lib.rs#L110
let maximum_likelihood_unconstrained:f64 =
Poisson::new(obs_rate_one * t_one ).unwrap().pmf(num_events_one as u64)
* Poisson::new(obs_rate_two * t_two ).unwrap().pmf(num_events_two as u64);
But now I need to use sample
. Trying to compile the sample
example code in the above (working) library makes it fail to build.
Here's a minimal example that also fails in the same way (you may recognize this as the example code from the documentation).
use rand::distributions::Distribution;
use statrs::distribution::Normal;
fn main() {
let mut r = rand::thread_rng();
let n = Normal::new(0.0, 1.0).unwrap();
for _ in 0..10 {
print!("{}", n.sample(&mut r));
}
}
fails:
error[E0599]: no method named `sample` found for struct `statrs::distribution::Normal` in the current scope
--> src/bin/docs_example.rs:9:24
|
9 | print!("{}", n.sample(&mut r));
| ^^^^^^ method not found in `statrs::distribution::Normal`
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
1 | use rand::distributions::Distribution;
|
warning: unused import: `rand::distributions::Distribution`
--> src/bin/docs_example.rs:1:5
|
1 | use rand::distributions::Distribution;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
The combination of "unused import for X" and "missing import: X" is really strange.
So, I've tried (in a new package) every code example I could find and none compile as instructed. I'm copy-pasting, but I do have to add main wrappers.
See here: https://github.com/jodavaho/statrs-test/tree/main/src/bin
In particular, Cargo.toml
[package]
name = "statrs-test"
version = "0.1.0"
authors = ["Josh"]
edition = "2018"
[dependencies]
statrs = "*"
# statrs = "0.13"
rand = "0.8.3"
Any thoughts on what is going on?
Looks like a crate version mismatch. statrs
only supports Rand 0.7, but you are trying to use it with Rand 0.8. The Distribution
trait you are importing is distinct from the Distribution
trait statrs
is using (because they are from different rand
crates), hence the unused import warning. (Unfortunately, the compiler message is confusing, because it does not tell you that it's talking about two different rand
crates.)
cargo tree
can help you to debug such problems.
Oh, I'm re-opening to show a workaround that got things to compile for me:
https://stackoverflow.com/q/67510812/389599
This is potentially worth documenting.
This is fixed with the new release.