ADVERTISEMENT
ADVERTISEMENT

Rust program to access a static variable in a module

Rust | Module Example: Write a program to access a static variable in a module.
Submitted by Nidhi, on October 30, 2021

Problem Solution:

In this program, we will create a static variable and then we will access created variable in a module using "use create".

Program/Source Code:

The source code to access a static variable in a module is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to access a static 
// variable in a module

pub static StaticVar: &str = "Hello World";

mod Sample {
use crate::StaticVar;

    pub fn sayHello() {
         println!("{}", StaticVar);
    }
}

fn main() {
   Sample::sayHello();
}

Output:

Hello World

Explanation:

In the above program, we created a static variable StaticVar. Then we created a module Sample, here we accessed the static variable StaticVar in module Sample.

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

Rust Modules Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.