英文:
Having confusion in understanding module structure in rust
问题
我正面临一些困惑,无法理解Rust模块的结构。正如Rust书中所述这里
> 在crate根文件中,你可以声明新的模块;
所以,正如Rust告诉我们的,crate的根是src/main.rs
和src/lib.rs
。
-
在上述链接中,书的作者在
garden.rs
文件中创建了一个名为garden
的模块,但这不是crate的根,怎么做到的? -
为了声明子模块,Rust书告诉我们:
> 在任何文件中,除了crate根文件,你都可以声明子模块。
这是否意味着要声明一个子模块,我们需要在src
文件夹中创建一个文件夹,然后在该文件夹内创建子模块的文件?就像这样描述的:
backyard
├── Cargo.lock
├── Cargo.toml
└── src
├── garden
│ └── vegetables.rs
├── garden.rs
└── main.rs
英文:
I am facing some confusion to understand the module's structure in rust.
As the rust-book mentioned here
> In the crate root file, you can declare new modules;
So, as rust tells us that crate root is src/main.rs
and src/lib.rs
.
-
In the above mentioned link the author of the book created the module named
garden
ingarden.rs
file which is not crate root, how??? -
For declaring submodules the rust books tells us this:
> In any file other than the crate root, you can declare submodules.
Does this means to declare a submodule we need to create a folder in src
folder and inside of that folder we need to create file for submodules? As describes here:
backyard
├── Cargo.lock
├── Cargo.toml
└── src
├── garden
│   └── vegetables.rs
├── garden.rs
└── main.rs
答案1
得分: 2
以下是翻译好的部分:
-
模块
garden
是通过在 crate 根文件(例如src/lib.rs
)中使用mod garden;
声明的。这意味着该模块是 crate 根中的一个项。 -
编译器会根据这个指令查找
src/garden.rs
或src/garden/mod.rs
。 -
无论编译器在那个文件中找到的源代码都会成为模块
crate::garden
的内容。
总的来说,模块总是由它们的父模块声明,而不是自己声明。
- 在这里,“子模块”指的只是“在另一个模块中的模块,而不是在 crate 根中的模块”。鉴于此,是的,子模块放在
src
文件夹内的文件夹中。
在示例中,由于 vegetables
是 garden
的子模块(意味着它在 garden.rs
中声明),
- 其模块路径是
crate::garden::vegetables
,和 - 其源代码位于
src/garden/vegetables.rs
。
英文:
> In the above mentioned link the author of the book created the module named garden in garden.rs file which is not crate root, how???
I think what is confusing you here is what “in a file” means.
The key thing to understand about the Rust module system is that a module is always declared in some Rust source code somewhere (whether it is the crate root or a module), and that is not the source code of the contents of the module. So, specifically:
-
The module
garden
is declared in the crate root file (such assrc/lib.rs
) bymod garden;
. This means the module is an item inside the crate root. -
The compiler takes this as instruction to look for
src/garden.rs
orsrc/garden/mod.rs
. -
Whatever source code the compiler finds in that file becomes the contents of the module known as
crate::garden
.
In general, modules are always declared by their parent, not themselves.
> Does this means to declare a submodule we need to create a folder in src folder and inside of that folder we need to create file for submodules?
By “submodule” here, the authors just mean “module that is in another module, rather than being in the crate root”. Given that: yes, submodules go in folders inside src
.
In the example, because vegetables
is a submodule of garden
(meaning that it was declared in garden.rs
),
- its module path is
crate::garden::vegetables
, and - its source code is found at
src/garden/vegetables.rs
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论