Rust 如何在doctest中使用自定义模块?

Rust 如何在doctest中使用自定义模块?,rust,rust-cargo,Rust,Rust Cargo,在main.rs中可以正常工作,但在simulation/factory.rs中的doctest中不能正常工作: mod simulation; use simulation::factory::FactoryType; 货物测试给出了错误信息 impl product_type::ProductType for FactoryType { /// Lorem Ipsum /// /// # Examples /// /// ``` /// u

main.rs
中可以正常工作,但在
simulation/factory.rs中的doctest中不能正常工作:

mod simulation;

use simulation::factory::FactoryType;
货物测试
给出了错误信息

impl product_type::ProductType for FactoryType {
    /// Lorem Ipsum
    ///
    /// # Examples
    ///
    /// ```
    /// use simulation::factory::FactoryType;
    ///
    /// ...
    /// ```
    fn human_id(&self) -> &String {
        ...
    }
}
----模拟::工厂::人工id\u 0标准输出----
:2:9:2:19错误:未解析的导入`simulation::factory::FactoryType`。可能是缺少了“外部板条箱模拟”?
:2使用simulation::factory::FactoryType;
^~~~~~~~~~
错误:由于上一个错误而中止
线程'simulation::factory::human_id_0'在'Box'处惊慌失措,/home/rustbuild/src/rust buildbot/slave/stable dist rustc linux/build/src/libsyntax/diagnostic.rs:192

如何让doctest工作?

正如huon dbaupp所指出的,不能从doc测试导入箱子

解决方案是将大部分代码定义为一个库板条箱,并有一个二进制文件,它只是一个外壳


例如,使用这种技术。

当您编写文档测试时,您必须像代码的用户那样行事。鉴于这些文件:

src/lib.rs

---- simulation::factory::human_id_0 stdout ----
    <anon>:2:9: 2:19 error: unresolved import `simulation::factory::FactoryType`. Maybe a missing `extern crate simulation`?
<anon>:2     use simulation::factory::FactoryType;
                 ^~~~~~~~~~
error: aborting due to previous error
thread 'simulation::factory::human_id_0' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:192
pub mod simulation {
    pub mod factory {
        pub struct FactoryType;

        impl FactoryType {
            /// ```
            /// use foo::simulation::factory::FactoryType;
            ///
            /// let f = FactoryType;
            /// assert_eq!(42, f.human_id())
            /// ```
            pub fn human_id(&self) -> u8 { 41 }
        }
    }
}
src/main.rs

---- simulation::factory::human_id_0 stdout ----
    <anon>:2:9: 2:19 error: unresolved import `simulation::factory::FactoryType`. Maybe a missing `extern crate simulation`?
<anon>:2     use simulation::factory::FactoryType;
                 ^~~~~~~~~~
error: aborting due to previous error
thread 'simulation::factory::human_id_0' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:192
pub mod simulation {
    pub mod factory {
        pub struct FactoryType;

        impl FactoryType {
            /// ```
            /// use foo::simulation::factory::FactoryType;
            ///
            /// let f = FactoryType;
            /// assert_eq!(42, f.human_id())
            /// ```
            pub fn human_id(&self) -> u8 { 41 }
        }
    }
}

一切正常。请注意,在main.rs中,您必须说
extern-crater
,然后所有引用都需要包含板条箱名称。doctest是相同的,除了
extern板条箱
会自动包含在doctest中。

如果您正在创建二进制文件(例如,如果其中有
src/main.rs
而不是
src/lib.rs
),则不能在doctest中使用其中的函数:doc测试将它们作为一个库导入板条箱(如果是一个)。请求帮助时,请花时间创建一个。正如您目前提出的问题,我们必须进行大量猜测才能确切知道存在什么。C#也是如此,直到他们将其用于EXE测试。来吧,铁锈,除掉陷阱。