错误 [E0599]:在当前作用域中找不到类型"&mut G"的名为"gen"的方法



我正在尝试使用QuickCheck Crate。

我已经实现了 Arbitrary Point {x: u32, y: u32}

impl Arbitrary for Point {
    fn arbitrary<G: Gen>(g: &mut G) -> Point {
        let x = g.gen::<u32>();
        let y = g.gen::<u32>();
        Point { x, y }
    }
}

编译器说:

error[E0599]: no method named `gen` found for type `&mut G` in the current scope
  --> src/main.rs:61:23
   |
61 |             let x = g.gen::<u32>();
   |                       ^^^
   |
   = note: the method `gen` exists but the following trait bounds were not satisfied:
           `&mut G : rand::Rng`
           `G : rand::Rng`
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
           `use rand::Rng;`

,但是我在测试模块中确实具有use rand:Rng;,而rand是我的货物中的DEV依赖性。

我在模块中也有use quickcheck::{quickcheck, Arbitrary, Gen};

为了创建任意发电机,我缺少什么?

---编辑---如果您想运行https://gist.github.com/russelldb/49b966ca2e23dfab8a0303090144735e4对我来说,它将重现问题。

这似乎是QuickCheck的问题。QuickCheck使用RAND版本0.6.5,而最新版本的RAND为0.7.0。

因为不同版本的特征不是兼容的rustc会给您这个错误。

要解决它,请在0.6.5版本中声明rand为依赖性。

相关内容