我是 Rust 的新手,来自 Python,有些事情的完成方式非常不同。在 Python 中,可以通过键入 from foo import bar
从 .py 文件导入单个函数,但我仍然没有在 Rust 中找到任何等效函数。
我有以下文件:
.
├── main.rs
└── module.rs
内容如下:
主要.rs
mod module;
fn main() {
module::hello();
}
模块.rs
pub fn hello() {
println!("Hello");
}
pub fn bye() {
println!("Bye");
}
如何创建我的模块或输入我的导入,这样我就不会收到以下警告:
warning: function is never used: `bye`
--> module.rs:5:1
|
5 | pub fn bye() {
| ^^^^^^^^^^^^
|
= note: #[warn(dead_code)] on by default
最佳答案
与导入模块、类型、函数和特征没有什么本质上的不同:
use path::to::function;
例如:
mod foo {
pub fn bar() {}
}
use foo::bar;
fn main() {
bar();
}
关于import - 如何从 Rust 中的模块导入单个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52377418/