Module 使嵌套模块在Rust中公开

Module 使嵌套模块在Rust中公开,module,rust,rust-cargo,Module,Rust,Rust Cargo,我正在开始一个学习生锈的项目,但我在最基本的事情上失败了,比如建立一个合适的模块结构。我的代码如下所示: // src/theorem/math.rs pub mod theorem { pub mod math { use std::ops::{Add, Sub}; pub struct Point { x: i32, y: i32, } impl Add for Poi

我正在开始一个学习生锈的项目,但我在最基本的事情上失败了,比如建立一个合适的模块结构。我的代码如下所示:

// src/theorem/math.rs
pub mod theorem {
    pub mod math {
        use std::ops::{Add, Sub};

        pub struct Point {
            x: i32,
            y: i32,
        }

        impl Add for Point {
            // Omitted...
        }
    }

    pub use math::{Point};
}

#[cfg(test)]
mod tests {
    use theorem::math::{Point};

    #[test]
    fn add_point() {
        let v1 = Point { x: 1, y: 1 };
        let v2 = Point { x: 2, y: 2 };
        assert_eq!(v1 + v1, v2);
    }
}
我试着
pub-use
,我试着在任何地方,任何东西面前写
pub
,但我得到的只是信息

error[E0432]: unresolved import `math::Point`
  --> src/theorem/math.rs:28:20
   |
28 |     pub use math::{Point};
   |                    ^^^^^ no `Point` in `math`
这是一个很好的见解,但对我没有帮助。我通读了文档,但是没有关于这个案例的真实例子,但是。。。一定有可能吧


我还尝试了一个合适的目录结构,比如
src/thermory/math/point.rs
,但这也不起作用。

您使用的编译器版本是什么,错误消息如下所示:

// src/theorem/math.rs
pub mod theorem {
    pub mod math {
        use std::ops::{Add, Sub};

        pub struct Point {
            x: i32,
            y: i32,
        }

        impl Add for Point {
            // Omitted...
        }
    }

    pub use math::{Point};
}

#[cfg(test)]
mod tests {
    use theorem::math::{Point};

    #[test]
    fn add_point() {
        let v1 = Point { x: 1, y: 1 };
        let v2 = Point { x: 2, y: 2 };
        assert_eq!(v1 + v1, v2);
    }
}
错误[E0432]:未解析的导入`math::Point`
--> :16:20
|
16 |使用数学:{点};
|^^^^^^^你是说“自我::数学”吗?

pub使用self::math::{Point}
实际上是您问题的解决方案!使用路径时,此路径始终是绝对路径。这意味着它是从你的板条箱的根来解释的。但是没有
math
模块作为根模块的直接子模块,因此出现了错误。

我使用的是1.16。但是谢谢你的提示,它现在似乎起作用了!