在理解Rust中的模块结构方面感到困惑。

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

Having confusion in understanding module structure in rust

问题

我正面临一些困惑,无法理解Rust模块的结构。正如Rust书中所述这里

> 在crate根文件中,你可以声明新的模块;

所以,正如Rust告诉我们的,crate的根是src/main.rssrc/lib.rs

  1. 在上述链接中,书的作者在garden.rs文件中创建了一个名为garden的模块,但这不是crate的根,怎么做到的?

  2. 为了声明子模块,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.

  1. 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???

  2. 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

以下是翻译好的部分:

  1. 模块 garden 是通过在 crate 根文件(例如 src/lib.rs)中使用 mod garden; 声明的。这意味着该模块是 crate 根中的一个项。

  2. 编译器会根据这个指令查找 src/garden.rssrc/garden/mod.rs

  3. 无论编译器在那个文件中找到的源代码都会成为模块 crate::garden 的内容。

总的来说,模块总是由它们的父模块声明,而不是自己声明。

  1. 在这里,“子模块”指的只是“在另一个模块中的模块,而不是在 crate 根中的模块”。鉴于此,是的,子模块放在 src 文件夹内的文件夹中。

在示例中,由于 vegetablesgarden 的子模块(意味着它在 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:

  1. The module garden is declared in the crate root file (such as src/lib.rs) by mod garden;. This means the module is an item inside the crate root.

  2. The compiler takes this as instruction to look for src/garden.rs or src/garden/mod.rs.

  3. 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.

huangapple
  • 本文由 发表于 2023年5月26日 00:30:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334456.html
匿名

发表评论

匿名网友

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

确定