不能在 Rust 文档测试中使用依赖包

Can't use a dependent crate in Rust documentation tests

我正在尝试为我用 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 .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/tokenize/mod.rs:

extern crate regex;
pub mod util;

(顶部)src/tokenize/util.rs:

extern crate regex; 
use regex::Regex;

我的项目布局有什么问题?

来自 The Rust Programming Language, first edition chapter on 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 点与此相关。当你有 no extern crate 行时,你的箱子会自动添加。添加第一个 extern crate 后,不会自动添加 crate — 包括您的 crate!

您需要为 regexrusty_nltk 添加 extern crate 行。

在被指向文档后,我通过用 extern crates:

在我的代码周围包装一个 main 来解决它
/// 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 .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,但如果这不是您的样式,您可以在代码前添加 # 以将其隐藏在文档中。