andrey
andrey

Reputation: 1585

Testing a custom crate

I've got this crate in/src/lib.rs which I'm trying to run tests on:

#![crate_type = "lib"]
#![crate_name = "mycrate"]

pub mod mycrate {
    pub struct Struct {
        field: i32,
    }

    impl Struct {
        pub fn new(n: i32) -> Struct {
            Struct { field: n }
        }
    }
}

The test file at /tests/test.rs:

extern crate mycrate;

use mycrate::*;

#[test]
fn test() {
    ...
}

Running cargo test gives this error:

tests/test.rs:3:5: 3:16 error: import `mycrate` conflicts with imported crate in this module (maybe you meant `use mycrate::*`?) [E0254]
tests/test.rs:3 use mycrate::*;
                     ^~~~~~~~~

What am I doing wrong here?

Upvotes: 0

Views: 177

Answers (1)

oli_obk
oli_obk

Reputation: 31163

A crate is also automatically a module of its own name. So you do not need to specify a sub-module. Since you imported everything in the mycrate crate, you also imported the mycrate::mycrate module, which caused the naming conflict.

Simply change the contents of your src/lib.rs to

pub struct Struct {
    field: i32,
}

impl Struct {
    pub fn new(n: i32) -> Struct {
        Struct { field: n }
    }
}

There's also no need for the crate_name and crate_type attributes.

Upvotes: 2

Related Questions