我在为我的库修复文档示例中的错误时遇到了困难。我的crate的文件结构如下:bignum
.
|-- Cargo.lock
|-- Cargo.toml
|-- examples
| |-- dat
| | `-- euler_13.dat
| |-- debug.rs
| `-- euler_13.rs
|-- README.md
|-- src
| |-- error.rs
| |-- inits.rs
| `-- lib.rs
在我的示例中,我的标题将类似于
// euler_13.rs
extern crate bignum;
use bignum::inits::Zero;
// ...
这个编译并且运行良好,但是现在当我在lib.rs
文档中写一个例子的时候,我似乎无法导入bignum::inits::Zero
//lib.rs
//...
impl BigNum {
//...
/// Constructs a ...
///
/// # Examples
///
/// ```
/// extern crate bignum;
/// use bignum::inits::Zero;
///
/// let a = bignum::BigNum::new(Zero::zero());
/// ```
///
pub fn new(base: BigNum) -> BigNum {
// ...
}
当我运行cargo test
时,我收到了这个错误
Running target/debug/lib-fe3dd7a75a504b04
running 3 tests
test crate_from_u32 ... ok
test create_from_string ... ok
test adding_no_carry ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured
Doc-tests bignum
running 1 test
test new_0 ... FAILED
failures:
---- new_0 stdout ----
<anon>:3:9: 3:15 error: unresolved import `self::bignum::inits::Zero`. Did you mean `self::self::bignum::inits`?
<anon>:3 use self::bignum::inits::Zero;
^~~~~~
error: aborting due to previous error
thread 'new_0' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:192
failures:
new_0
我看到了这个问题,但它处理的是从相同文件导入模块仍需要顶层范围。然而,在这里,我仍然使用bignum::
指定了顶层范围。
因此,虽然导入bignum::inits::Zero
对于我的所有测试和示例都有效,但对于我的文档却无效。为什么会这样?我尝试在前面添加self::
,但仍然收到相同的错误。如果我将示例更改为
extern crate bignum;
let a = bignum::BigNum::new(bignum::inits::Zero::zero());
它编译得很好。 但是,我该如何正确导入我的模块?
# Examples
中,extern create bignum
是隐含的,所以再次执行会创建错误。我提交了一个问题,建议更改此类错误的输出。 - Syntactic Fructose