Q:

Rust program to find the length of an array

belongs to collection: Rust Arrays Programs

0

In this program, we will create different types of arrays without specifying their type. Then we will find the length of the array using the len() function and print the result.

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 find the length of an array is given below. The given program is compiled and executed successfully.

// Rust program to find the 
// length of an array

fn main(){
   let arr1 = [5,10,15];
   let arr2 = [5.1,10.2,15.3,20.4];
   let arr3 = ["ABC","LMN","PQR","TUV","XYZ"];
   
   println!("Length of arr1: {}",arr1.len());
   println!("Length of arr2: {}",arr2.len());
   println!("Length of arr3: {}",arr3.len());
}

Output:

Length of arr1: 3
Length of arr2: 4
Length of arr3: 5

Explanation:

Here, we created three arrays arr1arr2, and arr3 with different data elements. Then we found the length of the array and printed the result.

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 initialize array elements with a d... >>
<< Rust program to create an array without specifying...