Q:

Rust program to initialize array elements with a default value

belongs to collection: Rust Arrays Programs

0

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

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 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 arr1arr2, and initialized them with default values. Then we printed the arrays.

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 access array elements using the \&... >>
<< Rust program to find the length of an array...