ADVERTISEMENT
ADVERTISEMENT

Rust program to create an array with constant size

Rust | Array Example: Write a program to create an array with constant size.
Submitted by Nidhi, on October 19, 2021

Problem Solution:

In this program, we will create an integer array with constant size. Then we will print 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.

// 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.

Rust Arrays Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.