【问题标题】:error[E0599]: no method named `gen` found for type `&mut G` in the current scope [duplicate]错误[E0599]:在当前范围内没有为类型`&mut G`找到名为`gen`的方法[重复]
【发布时间】:2019-11-16 00:34:30
【问题描述】:

我正在尝试使用quickcheck crate

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

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;,在我的 Cargo.toml 中有rand 作为开发依赖项。

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

为了创建任意生成器,我缺少什么?

--- 编辑 --- 如果您想为我运行https://gist.github.com/russelldb/49b96ca2e23dfab8a0f03090144735e4,它会重现问题。

【问题讨论】:

  • 请提供minimal reproducible example,因为您的代码对我来说看起来不错,所以错误一定在其他地方。
  • 顶部的use rand::Rng; 不能解决问题吗?
  • 我有一个使用 rand::Rng;。我会尝试发布一个 MRE。
  • 这里有一个最小的(小?)repro ex gist.github.com/russelldb/49b96ca2e23dfab8a0f03090144735e4。提前致谢。
  • @russelldb 请将其包含在此问题中,而不是使用 github。请edit你的问题:)

标签: rust quickcheck


【解决方案1】:

这似乎是快速检查的问题。 Quickcheck uses rand version 0.6.5,而最新版本的rand is 0.7.0

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

要解决它,在 0.6.5 版本中将 rand 声明为依赖项,它就会起作用。

【讨论】:

  • 太好了,解决了,非常感谢。那么如果 quickcheck 依赖于 rand,为什么我需要声明对它的依赖呢?
猜你喜欢
  • 2017-03-22
  • 2018-07-08
  • 1970-01-01
  • 2023-01-15
  • 2017-03-16
  • 2021-05-13
  • 2021-03-23
  • 2021-02-28
  • 1970-01-01
相关资源
最近更新 更多