5

I'm fairly new to Rust and coming from Python there are some things that are done very differently. 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.

I have the following files:

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

With the following contents:

main.rs

mod module;

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

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
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    It seems like you should clarify your question. Is the issue `hello` or `bye`? Why are both included, if the question is about importing `bye`? The warning is expected because `bye` is never used right now. – loganfsmyth Sep 18 '18 at 00:48
  • 2
    Your second unrelated question is probably answered by [How to disable unused code warnings in Rust?](https://stackoverflow.com/q/25877285/155423) although the true answer for "how do I get rid of unused code" is to *delete that code*. – Shepmaster Sep 18 '18 at 00:59
  • 1
    In this particular case, if you have some reason to keep the unused function, you can get rid of the warning by making the module public: `pub mod module`. Rust does not warn about public unused code by default, since the assumption is that these functions could be used by other projects depending on your code as a library. – Sven Marnach Sep 18 '18 at 09:47

1 Answers1

11

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();
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366