如何在 Rust 中“使用另一个文件”?带有连字符的模块

ami*_*min 3 rust

another_file在 Rust 中使用下划线作为单词分隔符工作正常。

我如何改用连字符 ( another-file.rs)?

// another-file.rs
pub fn method() { }
Run Code Online (Sandbox Code Playgroud)
// lib.rs
use another_file;        // <-- ERROR can not find another_file.rs
another_file::method();
Run Code Online (Sandbox Code Playgroud)

mca*_*ton 5

您必须显式声明模块并提供其路径

#[path="../path/to/another-file.rs"]
mod another_file;

use another_file;
Run Code Online (Sandbox Code Playgroud)

然而,这并不常见,我不会推荐它。只需坚持使用 snake_case 作为模块名称。