Rust program to create a variable to store data in the HEAP area

Rust Example: Write a program to create a variable to store data in the HEAP area.
Submitted by Nidhi, on November 25, 2021

Problem Solution:

In this program, we will create a variable that will allocate space into the HEAP area of memory using the Box::new() function and print the result.

Program/Source Code:

The source code to create a variable to store data in the HEAP area is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to create a variable 
// to store data in the HEAP area

fn main() {
   let number = Box::new(5); 
   println!("Number: {}", number);
}

Output:

Number: 5

Explanation:

In the main() function, we used Box::new() function to create a variable in the HEAP area of memory. After that, we printed the value of the variable.

Rust Miscellaneous Programs »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.