在 Rust 中,mod.rs 文件的用途是什么? [英] In Rust, what is the purpose of a mod.rs file?

查看:62
本文介绍了在 Rust 中,mod.rs 文件的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我见过的一些 Rust 项目中(即 pczarn/rustboot),我见过 <无论出于何种原因,目录中的 code>mod.rs 文件.我一直没能找到关于这个的文档,而且我在许多其他 Rust 项目中看到过它.mod.rs 文件的用途是什么,我应该什么时候使用它?

解决方案

想象以下目录结构:

<前>代码/`- main.rs- 某物/`- mod.rs

如果在 main.rs 中你做了 mod something;,那么它会在 something/mod.rs 文件中查找使用作为 something 的模块声明的内容.

替代方法是在 code/ 目录中有一个 something.rs 文件.

总结一下,当你写一个空的模块声明,比如 mod something; 时,它会出现在:

  • 同一目录中名为 something.rs 的文件
  • 在同一目录中名为 something 的文件夹中名为 mod.rs 的文件

然后使用这些文件中的任何一个的内容作为模块声明的内容.

In some Rust projects I've seen (i.e pczarn/rustboot), I've seen mod.rs files in directories for whatever reason. I've not been able to find documentation about this, and I've seen it in many other Rust projects. What is the purpose of a mod.rs file, and when should I use it?

解决方案

Imagine the following directory structure:

code/
  `- main.rs
   - something/
     `- mod.rs

If in main.rs you do mod something;, then it'll look in the something/mod.rs file to use as the contents of the module declaration for something.

The alternative to this is to have a something.rs file in the code/ directory.

So to recap, when you write an empty module declaration such as mod something;, it looks either in:

  • a file called something.rs in the same directory
  • a file called mod.rs in a folder called something in the same directory

It then uses the contents of either of those files to use as the contents of the module declaration.

这篇关于在 Rust 中,mod.rs 文件的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆