Rust模块在不同文件中多次使用时的奇怪行为

huangapple go评论62阅读模式
英文:

Rust module weird behavior with multiple use from different file

问题

我以为我理解Rust模块,但对于mod的不一致性,至少对于我的理解水平来说,我完全迷失了。

我在项目中有以下文件夹结构:

project
|
 - src
   |- main.rs
   |- mod.rs
   |- common
     |- error.rs
     |- global.rs
     |- ids.rs
     |- mod.rs
   - user_info
     |- user.rs
     |- admin.rs
     |- mod.rs
   - data_source
     |- data_src.rs
     |- data_bridge.rs
     |- data_copies.rs
     |- mod.rs

正如你所见,所有文件夹都有一个mod.rs文件,其中基本上包含对其中的所有子文件夹或文件的pub mod声明。我以为这样可以使导入更清晰。

data_src.rs使用以下方式导入模块:

use super::*;
use super::super::common::*;

data_src.rs明确依赖于以下模块:

data_source/data_bridge.rs
common/error.rs
common/global.rs

data_copies.rs使用以下方式导入模块:

use super::*;
use super::super::common::*;

并且data_copies.rs依赖于以下模块:

data_source/data_src.rs
data_source/data_bridge.rs
common/error.rs
common/global.rs
common/ids.rs

在使用cargo build命令进行编译时,我在data_src.rs中没有看到任何错误,但在data_copies.rs中却不是这样,我一直看到相同的错误:

use of undeclared crate or module "..."

来自所有导入的函数中。这对我来说非常不合理,因为我在data_src.rs文件中使用相同的use命令导入模块,但只在data_copies.rs中出现错误。

我查找了在线资源,但找不到任何理论或解释能帮助我理解问题。有人能告诉我发生了什么吗?

英文:

I thought I understood Rust modules, but I am completely lost with mod inconsistencies, at least with my own level of understanding.

I have the following folder structure in my project:

project
|
 - src
   |- main.rs
   |- mod.rs
   |- common
     |- error.rs
     |- global.rs
     |- ids.rs
     |- mod.rs
   - user_info
     |- user.rs
     |- admin.rs
     |- mod.rs
   - data_source
     |- data_src.rs
     |- data_bridge.rs
     |- data_copies.rs
     |- mod.rs

As you may see all folders have a mod.rs file, which basically contains pub mod declaration to all the sub-folders or folder-level files in it. Thought that would make importing cleaner.

data_src.rs is importing modules using

use super::*;
use super::super::common::*

data_src.rs is specifically has dependencies on

data_source/data_bridge.rs
common/error.rs
common/global.rs

data_copies.rs is importing modules using

use super::*;
use super::super::common::*

And data_copies.rs has dependencies on

data_source/data_src.rs
data_source/data_bridge.rs
common/error.rs
common/global.rs
common/ids.rs

While compiling using cargo build command I am seeing no errors from data_src.rs, but same is not true for data_copies.rs, where I keep seeing the same error:

use of undeclared crate or module "..."

across all the imported functions from all the files.

This is very unreasonable for me, because I am using the same use command to import mods in data_src.rs file where it does not throw any error, but only in data_copies.rs.

I checked online, but I cannot find any theaory or explaination remotely helping me understand the problem. Can someone tell me what is going on.

答案1

得分: 1

Rust有两种类型的包,二进制和库。

一个二进制包是一个具有入口点并直接在机器上运行的包。库是一个没有入口点的包,可以在其他包中使用。

当你编写包时,Cargo从主文件中开始读取它,对于二进制包来说,主文件是src/main.rs,对于库来说,主文件是src/lib.rs

尽管模块是通过与模块同名的rs文件或包含mod.rs文件的文件夹来声明的,但顶级模块需要在main文件中声明,这就是你的问题。

你的问题在于你将顶级声明放在了src/mod.rs中,而Cargo不知道如何导入它们,你只需要将顶级声明移到src/main.rs,而不是src/mod.rs中。

英文:

Rust has two kinds of crates, binaries and libraries.

A binary is a crate that has an entry point and runs directly in the machine. And library is a crate that doesn't has an entry point, is a crate that can be used in other crates.

When you're writing crates cargo starts reading it from the main file, which is src/main.rs for binaries, and src/lib.rs for libraries.

Although the modules are declared through rs files which same name as the module, or through a folder with a mod.rs file. The top level modules need be declared in the main file, and this is your problem.

Your problem is you're declaring your top level in src/mod.rs and cargo don't know how to import them, all you need is move your top level declaration to src/main.rs instead of src/mod.rs.

huangapple
  • 本文由 发表于 2023年3月4日 09:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定