为什么在Rust中有一个mod关键字? [英] Why is there a mod keyword in Rust?

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

问题描述

阅读 我想知道为什么会有"mod"关键字和mod.rs?文件夹层次结构也可以描述模块吗?

After reading this I'm wondering why is there "mod" keyword and mod.rs? The folder hierarchy can describe the module as well?

推荐答案

必须明确声明模块的原因有两个:

There are a couple of reasons why modules must be explicitly declared:

  • 模块可以是公共的(pub mod foo;)或私有的(mod foo;).

可以对它们应用属性,这些属性不能位于文件内部;有两个主要示例:#[path = "x.rs"]指定不同的路径,和#[cfg(…)]用于条件编译,用于模块无法解析或宏扩展的情况.

They can have attributes applied to them, attributes that couldn’t sit inside the file; there are two primary examples of that: #[path = "x.rs"] specifying a different path, and #[cfg(…)], for conditional compilation, for cases where the module would fail to parse or have its macros expand.

这篇关于为什么在Rust中有一个mod关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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