Rust program to find the length of an array

Rust | Array Example: Write a program to find the length of an array.
Submitted by Nidhi, on October 19, 2021

Problem Solution:

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.

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 arr1, arr2, and arr3 with different data elements. Then we found the length of the array and printed the result.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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