[英]How to import a function in main.rs in Rust
這可能是一個愚蠢的問題,但我似乎無法解決這個問題。
我有這種文件結構:
└── src
├── another.rs
├── some_file.rs
└── main.rs
在some_file.rs
中,我想在main.rs
中調用 function 。 所以,我嘗試在some_file.rs
中做這樣的事情:
use crate::main
fn some_func() {
// other code
main::another_func_in_main();
}
但是編譯器會拋出一個錯誤:
use of an undeclared crate or module `main`
我該如何解決這個問題?
沒有main
模塊,即使您有一個main.rs
文件。 你放在main.rs
文件中的東西被認為是 crate 的根目錄。
所以你有兩種方法可以調用 function:
crate::another_func_in_main();
use crate::another_func_in_main();
// Then in code, no need for a prefix:
another_func_in_main();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.