mod sample;
use sample::sample::sayHello;
fn main() {
sayHello("Herry Potter".to_string());
}
Output:
Compile:
$ rustc main.rs
Execute:
$ ./main
Hello Herry Potter
Explanation:
In the above program, we created a module Sample in the "sample.rs" file. The Sample module contains a function sayHello() to print the message on the console screen. Then we imported the file using the mod keyword in the "main.rs" file.
In the main() function, we called the sayHello() method with a specified string and printed the result.
Program/Source Code:
The source code to import a module from a different file is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.
sample.rs
main.rs
Output:
Explanation:
In the above program, we created a module Sample in the "sample.rs" file. The Sample module contains a function sayHello() to print the message on the console screen. Then we imported the file using the mod keyword in the "main.rs" file.
In the main() function, we called the sayHello() method with a specified string and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer