简体   繁体   English

如何从Rust中的模块导入单个功能?

[英]How can I import a single function from a module in Rust?

I'm fairly new to Rust and coming from Python there are some things that are done very differently. 我对Rust还是很陌生,来自Python,有些事情做的非常不同。 In Python, one can import a single function from a .py file by typing from foo import bar , but I still haven't found any equivalent in Rust. 在Python中,可以通过from foo import bar键入内容来from foo import bar .py文件中导入单个函数,但是我仍然没有在Rust中找到任何等效的函数。

I have the following files: 我有以下文件:

.
├── main.rs
└── module.rs

With the following contents: 具有以下内容:

main.rs main.rs

mod module;

fn main() {
    module::hello();
}

module.rs module.rs

pub fn hello() {
    println!("Hello");
}

pub fn bye() {
    println!("Bye");
}

How do I create my module or type my imports so that I don't get the following warning: 我如何创建模块或输入导入,以免收到以下警告:

warning: function is never used: `bye`
  --> module.rs:5:1
   |
 5 |     pub fn bye() {
   |     ^^^^^^^^^^^^
   |
   = note: #[warn(dead_code)] on by default

There's nothing materially different from importing a module vs a type vs a function vs a trait: 与导入模块,类型,函数,特征没有本质上的区别:

use path::to::function;

For example: 例如:

mod foo {
    pub fn bar() {}
}

use foo::bar;

fn main() {
    bar();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM