[英]How to import a module from a directory outside of the `src` directory?
我在學習如何訪問模塊時被卡住了。 我正在嘗試將src
以外的文件夾插入src
。 它不起作用,它給了我一個錯誤。 這是我的項目樹。
$ Project1
.
|-- src
| |-- main.rs
| |--FolderinSrcFolder
| |--folderinsrcmodule.rs
|
|--anothersrc
| |--mod.rs
|
|-- rootmodule.rs
|-- Cargo.toml
|-- Cargo.lock
如何訪問anothersrc/mod.rs
src/main.rs
? 如何從src/main.rs
訪問rootmodule.rs
?
我已經閱讀了 Rust 文檔。
不要。 將所有源代碼放入src
目錄。 你也可以用它自己的src
目錄創建另一個 crate。 不要與這些成語和慣例作斗爭,這根本不值得。
也可以看看:
這直接回答了您的問題,但我強烈建議您不要實際使用它!
布局
.
├── Cargo.toml
├── bad_location.rs
└── src
└── main.rs
src/main.rs
#[path = "../bad_location.rs"]
mod bad_location;
fn main() {
println!("Was this a bad idea? {}", bad_location::dont_do_this());
}
錯誤定位.rs
pub fn dont_do_this() -> bool {
true
}
關鍵是#[path]
注釋。
也可以看看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.