2015-12-19 63 views
4

我正在嘗試爲我在Rust中編寫的項目編寫文檔。其中一個文檔需要使用regex::Regex。下面是我想要寫的文檔:不能在Rust文檔測試中使用相關的箱子

/// Return a list of the offsets of the tokens in `s`, as a sequence of `(start, end)` 
/// tuples, by splitting the string at each successive match of `regex`. 
/// 
/// # Examples 
/// 
/// ``` 
/// use rusty_nltk::tokenize::util::regexp_span_tokenize; 
/// use regex::Regex; 
/// 
/// let s = "Good muffins cost $3.88\nin New York. Please buy me 
///   two of them.\n\nThanks."; 
/// let regex = regex::Regex::new(r"\s").unwrap(); 
/// let spans = regexp_span_tokenize(s, regex).unwrap(); 
/// ``` 

它給我這個錯誤:

---- tokenize::util::regexp_span_tokenize_0 stdout ---- 
    <anon>:4:9: 4:14 error: unresolved import `regex::Regex`. Maybe a missing `extern crate regex`? [E0432] 
<anon>:4  use regex::Regex; 
       ^~~~~ 
error: aborting due to previous error 

但是當我添加extern crate regex;,我得到這個錯誤:

---- tokenize::util::regexp_span_tokenize_0 stdout ---- 
    <anon>:3:9: 3:19 error: unresolved import `rusty_nltk::tokenize::util::regexp_span_tokenize`. Maybe a missing `extern crate rusty_nltk`? [E0432] 
<anon>:3  use rusty_nltk::tokenize::util::regexp_span_tokenize; 
       ^~~~~~~~~~ 
<anon>:4:9: 4:14 error: unresolved import `regex::Regex`. Did you mean `self::regex`? [E0432] 
<anon>:4  use regex::Regex; 
       ^~~~~ 
error: aborting due to 2 previous errors 

一些相關的部分相關文件是:

src/lib.rs:

extern crate regex; 
pub mod tokenize; 

的src /記號化/ mod.rs:

extern crate regex; 
pub mod util; 

(頂)的src /記號化/ util.rs:

extern crate regex; 
use regex::Regex; 

我在做什麼毛病的佈局我項目?

回答

6

The Rust Programming Language一章documentation

Here's the full algorithm rustdoc uses to preprocess examples:

  1. Any leading #![foo] attributes are left intact as crate attributes.
  2. Some common allow attributes are inserted, including unused_variables , unused_assignments , unused_mut , unused_attributes , and dead_code . Small examples often trigger these lints.
  3. If the example does not contain extern crate , then extern crate <mycrate>; is inserted.
  4. Finally, if the example does not contain fn main , the remainder of the text is wrapped in fn main() { your_code }

點#3是與此有關。當你有沒有extern crate行,你的箱子自動添加。一旦添加第一個extern crate,,將不會自動添加包裝箱 - 包括您的箱子!

您需要爲regexrusty_nltk添加extern crate行。

+2

我剛剛發佈這個答案。我會給出這個答案,並將我的解決方案作爲單獨的答案添加。 – erip

1

所指向的文檔後,我解決它通過圍繞我的代碼mainextern crate S:

/// Return a list of the offsets of the tokens in `s`, as a sequence of `(start, end)` 
/// tuples, by splitting the string at each successive match of `regex`. 
/// 
/// # Examples 
/// 
/// To return a list of spans based on whitespaces: 
/// 
/// ``` 
/// extern crate regex; 
/// extern crate rusty_nltk; 
/// use rusty_nltk::tokenize::util::regexp_span_tokenize; 
/// use regex::Regex; 
/// 
/// fn main() { 
/// let s = "Good muffins cost $3.88\nin New York. Please buy me 
///   two of them.\n\nThanks."; 
/// let regex = Regex::new(r"\s").unwrap(); 
/// let spans = regexp_span_tokenize(s, &regex); 
/// } 
/// ``` 

我決定改變我的文檔樣式包括main獻給所有的例子,但如果這不是你的風格,你可以在代碼之前添加#來隱藏文檔。