Rust program to create a simple module

Rust | Module Example: Write a program to create a simple module.
Submitted by Nidhi, on October 29, 2021

Problem Solution:

In this program, we will create a module that contains a function. A module is a group of similar types of functions.

Program/Source Code:

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

// Rust program to create a simple module

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

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

Output:

Hello Herry Potter

Explanation:

In the above program, we created a module Sample. The Sample module contains a function sayHello() to print the message.

In the main() function, we called the sayHello() method with a specified string and printed the result.

Rust Modules Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.