英文:
Is it essential to declare "mod" for modules in Rust?
问题
I have translated the directory structure and relevant code portions for you:
我有以下的目录结构:
├── myappl
│ ├── Cargo.lock
│ ├── Cargo.toml
│ ├── src
│ │ └── main.rs
│ └── target
│ ├── CACHEDIR.TAG
│ └── debug
│ ├── build
│ ├── deps
...
...
...
└── myfns
├── Cargo.toml
└── src
└── lib.rs
在 myfns/src/lib.rs 中,我只有以下代码:
pub fn afn(){
println!("in myfns::afn");
}
在 myappl 目录中,我执行了以下命令:
Cargo add myfns --path ../myfns
在 myappl/src/main.rs 中,我有以下代码:
use myfns::*;
fn main(){
afn();
}
然后我构建并运行 myappl:
cargo run myappl
尽管 myfns 中没有像[此处示例][1]中所示的模块声明,上述方案仍然有效。
我应该在 myfns/src/lib.rs 中添加 `mod` 关键字吗?
pub mod myfns {
pub fn afn(){
println!("in myfns::afn");
}
}
声明模块时使用 `mod` 关键字的好处是什么?
[1]: https://doc.rust-lang.org/rust-by-example/mod/visibility.html
If you have any further questions or need additional translations, please let me know.
英文:
I have following directory structure:
├── myappl
│ ├── Cargo.lock
│ ├── Cargo.toml
│ ├── src
│ │ └── main.rs
│ └── target
│ ├── CACHEDIR.TAG
│ └── debug
│ ├── build
│ ├── deps
...
...
...
└── myfns
├── Cargo.toml
└── src
└── lib.rs
In myfns/src/lib.rs, I only have:
pub fn afn(){
println!("in myfns::afn");
}
In myappl directory, I give command:
Cargo add myfns --path ../myfns
In myappl/src/main.rs, I have:
use myfns::*;
fn main(){
afn();
}
Then I build and run myappl:
cargo run myappl
Above scheme works, despite there being no module declaration in myfns as given in example here.
Should I add mod
keyword in myfns/src/lib.rs:
pub mod myfns {
pub fn afn(){
println!("in myfns::afn");
}
}
What is the advantage of declaring modules by mod
keyword?
答案1
得分: 3
你这里没有任何模块,所以不需要使用 mod
关键字。你的代码结构是有两个 crate,一个是 bin
crate,一个是 lib
crate。这两个 crate 可以相互使用,无需使用任何 mod
关键字,因为它们都没有 mod
。afn
函数是在你的 lib
crate 的顶层声明的。
如果你想要在同一个 crate 中将 afn
函数放在一个单独的文件中,那么需要使用 mod
关键字,就像这样:
└── myappl
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── main.rs
| └── myfns.rs
如果 myfns.rs
包含以下内容:
pub fn afn(){
println!("in myfns::afn");
}
你的 main 函数应该是:
mod myfns;
fn main(){
myfns::afn();
}
或者:
mod myfns;
use myfns::afn;
fn main(){
afn();
}
模块用于代码结构化。当然,它们不是必需的,你可以将所有代码写在一个单独的文件中。但如果你想要在多个文件中组织代码而不创建多个 crates,那么模块是你想要的工具。
英文:
You are not having any mods here, so you don't need the mod
keyword. The way your code is set up is that you have two crates, one bin
crate and one lib
crate. Those two crates can of course use each other without any mod
keywords, because neither of them have a mod
, the afn
function is declared in the top level of your lib
crate.
You need the mod
keyword if you want to have the afn
function in a separate file in the same crate as the main
. Like so:
└── myappl
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── main.rs
| └── myfns.rs
If myfns.rs
contains:
pub fn afn(){
println!("in myfns::afn");
}
Your main has to be:
mod myfns;
fn main(){
myfns::afn();
}
or:
mod myfns;
use myfns::afn;
fn main(){
afn();
}
Mods are for code structuring. Of course they aren't necessary, you can write everything in a single file. But if you want to organize your code in multiple files without creating many crates, mods are what you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论