Q:

Rust program to create the nested module

belongs to collection: Rust Modules Programs

0

In this program, we will create a module Sample that contains nested module Nested. Then we will create a function sayHello() inside the Nested module.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create a nested module is given below. The given program is compiled and executed successfully.

//Rust program to create nested module

pub mod Sample {
   pub mod Nested{
        pub fn sayHello(name:String) {
            println!("Hello {}",name);
        }
   }
}

use Sample::Nested::sayHello;
fn main(){
   sayHello("Herry Potter".to_string());
}

Output:

Hello Herry Potter

Explanation:

In the above program, we created a module Sample. The Sample module contains a nested module Nested. The Nested module contains a function sayHello() to print the message. And, we imported sayHello() function using the below statement.

use Sample::Nested::sayHello;

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

total answers (1)

Rust program to import a module from a different f... >>
<< Rust program to import a module using the \'u...