Rust program to create an array with constant size

Rust | Array Example: Write a program to create an array with constant size.
Submitted by Nidhi, on October 19, 2021

Problem Solution:

In this program, we will create an integer array with constant size. Then we will print array elements.

Program/Source Code:

The source code to create an array with the constant size is given below. The given program is compiled and executed successfully.

// Rust program to create an array 
// with constant size

fn main() {
/*
   // The below code will generate error, 
   // we cannot specify variabl for array size 
   let SIZE: usize = 5; 
   let mut intArr:[i32;SIZE] = [1,2,3,4,5];
*/   
   const SIZE: usize = 5; 
   let mut intArr:[i32;SIZE] = [1,2,3,4,5];
   
   println!("Array elements:");
   for i in 0..5 {
      print!("{} ",intArr[i]);
   }
}

Output:

Array elements:
1 2 3 4 5

Explanation:

Here, we created a constant SIZE with value 5 and also created an array intArr with the size of created constant SIZE. Then we printed the array elements.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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