简体   繁体   English

使用子商品时如何避免“源特征是私密的”?

[英]How do I avoid 'source trait is private' when using subtraits?

I'm trying to use quickcheck in Rust. 我正在尝试在Rust中使用quickcheck I want to define my enum as an instance of Arbitrary , so I can use it in tests. 我想将我的枚举定义为Arbitrary的实例,所以我可以在测试中使用它。

#![feature(plugin)]
#![plugin(quickcheck_macros)]

#[cfg(test)]
extern crate quickcheck;

use quickcheck::{Arbitrary,Gen};

#[derive(Clone)]
enum Animal {
    Cat,
    Dog,
    Mouse
}

impl Arbitrary for Animal {
    fn arbitrary<G: Gen>(g: &mut G) -> Animal {
        let i = g.next_u32();
        match i % 3 {
            0 => Animal::Cat,
            1 => Animal::Dog,
            2 => Animal::Mouse,
        }
    }
}

However, this gives me a compilation error: 但是,这给了我一个编译错误:

src/main.rs:18:17: 18:29 error: source trait is private
src/main.rs:18         let i = g.next_u32();
                               ^~~~~~~~~~~~

What causes this error? 是什么导致这个错误? I know there's this rust issue but since Gen is imported I would think I could call .next_u32 . 我知道有这个生锈问题但是因为Gen是导入的,我想我可以调用.next_u32

It looks like Gen has rand::Rng as a parent trait, the above works if you add extern crate rand after adding rand = "*" to your Cargo.toml. 看起来Genrand::Rng作为父特征,如果你在你的Cargo.toml添加rand = "*"后添加extern crate rand ,上面的工作方法就有效。

[I also had to remove the #[cfg(test)] above the quickcheck import] [我还必须删除quickcheck导入上面的#[cfg(test)] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM