2016-01-11 88 views
0

我在/src/lib.rs了這個箱子,我正在嘗試運行測試:測試定製的箱子

#![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 } 
     } 
    } 
} 

/tests/test.rs測試文件:

extern crate mycrate; 

use mycrate::*; 

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

運行cargo test給出了這樣的錯誤:

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::*; 
        ^~~~~~~~~ 

我在做什麼錯在這裏?

回答

2

箱子也自動成爲自己名稱的模塊。所以你不需要指定一個子模塊。由於您導入了mycrate包中的所有內容,因此您還導入了導致命名衝突的mycrate::mycrate模塊。

src/lib.rs的內容,只要切換到

pub struct Struct { 
    field: i32, 
} 

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

但也沒有必要爲crate_namecrate_type屬性。