2015-06-24 22 views
4

我正在玩一個用於2D噪聲生成的小箱子。這是我的「lib.rs」文件的一個簡單的代碼片段:在文檔測試中使用本地模塊時出錯

pub mod my_math { 
    pub struct Vec2<T> { 
     ... 
    } 
    ... 
} 
pub mod my_noise { 
    use num::Float; 
    use std::num::Wrapping; 
    use my_math::*; 

    /// Gets pseudo-random noise based on a seed vector. 
    /// 
    /// # Examples 
    /// 
    /// ``` 
    /// use my_math::Vec2; 
    /// 
    /// let v_seed = Vec2::<f32>::new_values(4.134, -23.141); 
    /// let noise_val = get_noise_white(&v_seed); 
    /// 
    /// assert!(noise_val >= 0.0); 
    /// assert!(noise_val <= 1.0); 
    /// ``` 
    pub fn get_noise_white(seed: &Vec2<f32>) -> f32 { 
     ... 
    } 
} 

然而,當我運行貨物檢驗,我得到以下錯誤:

---- my_noise::get_noise_white_0 stdout ----

<anon>:3:9: 3:16 error: unresolved import my_math::Vec2 . Maybe a missing extern crate my_math ?

<anon>:3 use my_math::Vec2;

我也嘗試過其他形式的文檔評論中的use聲明,其中包括use my_math::*;use self::my_math::*;。如果我完全刪除該行,那麼我得到的錯誤是Vec2未定義。

這樣做的正確方法是什麼?

回答

5

您必須指定箱子的頂級域名(姑且稱之爲MYLIB):

use mylib::my_math::Vec2; 

的理由是,您的文檔例如必須是可用的,是你的庫的客戶端。如果你把自己放在鞋裏,他們會拿取你的圖書館(通常是貨物,但沒關係),然後把extern crate mylib放在他們的頂層lib.rs/main.rs中。然後,爲了使用你的庫的一部分,他們必須指定完全限定的名字才能使用它的子項。

而這正是你在你的rustdoc測試評論中必須做的。

另外,我認爲值得引用Rust書的相關部分Documentation as tests,它解釋了一些適用於doc代碼片段的小修改。其中之一是:

If the example does not contain extern crate , then extern crate <mycrate>; is inserted.

+0

謝謝,修好了! – heyx3

相關問題