Q:

Rust program to create an array with constant size

belongs to collection: Rust Arrays Programs

0

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

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 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.

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

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to compare two arrays using the equal... >>
<< Rust program to pass an array into the function us...