Rust program to initialize array elements with a default value

Rust | Array Example: Write a program to initialize array elements with a default value.
Submitted by Nidhi, on October 19, 2021

Problem Solution:

In this program, we will create two arrays and initialize them with default values. Then we will print the arrays.

Program/Source Code:

The source code to initialize array elements with a default value is given below. The given program is compiled and executed successfully.

// Rust program to initialize array 
// elements with a default value

fn main(){
   let arr1:[i32;5] = [-20;5];
   let arr2:[f32;5] = [-123.45;5];
   
   println!("Array1:\n{:?}",arr1);
   println!("Array2:\n{:?}",arr2);
}

Output:

Array1:
[-20, -20, -20, -20, -20]
Array2:
[-123.45, -123.45, -123.45, -123.45, -123.45]

Explanation:

Here, we created two arrays arr1, arr2, and initialized them with default values. Then we printed the arrays.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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