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