gpt4 book ai didi

module - 在 Rust 中公开嵌套模块

转载 作者:行者123 更新时间:2023-11-29 08:14:21 25 4
gpt4 key购买 nike

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

// 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/theorem/math/point.rs 这样的适当目录结构,但这也没有用。

最佳答案

您使用什么编译器版本? Since version 1.13 ,错误消息如下所示:

error[E0432]: unresolved import `math::Point`
--> <anon>:16:20
|
16 | pub use math::{Point};
| ^^^^^ Did you mean `self::math`?

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

关于module - 在 Rust 中公开嵌套模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com