在Rust文档测试中无法使用依赖包

时间:2015-12-19 15:36:24

标签: rust rust-crates rustdoc

我试图为我在Rust写作的项目编写文档。其中一个文档需要使用regex::Regex。这是我试图写的文件:

/// 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 / tokenize / util.rs:

extern crate regex; 
use regex::Regex;

我的项目布局出了什么问题?

1 个答案:

答案 0 :(得分:3)

在被指向文档之后,我通过main在我的代码周围包裹extern crate来解决它:

/// 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);
/// }
/// ```

我决定更改我的doc样式以包含所有示例的main,但如果这不是您的样式,则可以在代码之前添加#以将其从文档中隐藏。