首页 > 解决方案 > 如何在同一级别包含文件/模块?

问题描述

我正在使用具有以下文件结构的 Rust 2018:

src/main.rs
src/types.rs
src/compute.rs

types.rs包含pub struct Entity {}.

我没有问题,包括types和:computemain.rs

mod types;
mod compute;

我想在types里面导入compute.rs

mod types;
// or
pub mod types;

我得到:

file not found for module types 

我试图遵循版本指南中的路径清晰但没有成功。

这些示例从不引用不是来自或的同一级别导入main.rslib.rs

我尝试使用以下方法指定路径:

#[path = "types.rs"] mod types;

然后我得到这个错误:

expected type `compute::types::Entity`
              found type `types::Entity`

似乎它正在重新定义types我的模块中的模块compute

我不能使用use关键字,因为它不是外部 crate。

我通过示例查看了 Rust 中的文件层次结构,但没有相同级别的导入。

我试图避免mod.rs在 Rust 2015 中出现类似情况。

  1. 有没有办法进行同级别的导入?

  2. 我的文件结构有问题吗?

  3. 什么文件结构会更好?

标签: rust

解决方案


推荐阅读