Rust program to create a vector that can store only integers

Rust Example: Write a program to create a vector that can store only integers.
Submitted by Nidhi, on November 27, 2021

Problem Solution:

In this program, we will create a vector that can store only integer elements. Then, we will store some more elements using the push() function into vector.

Program/Source Code:

The source code to create a vector that can store only integers is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to create a vector 
// that can store only integers

fn main(){
   let mut int_vec: Vec<i32> = vec![10,20];

   int_vec.push(30);
   int_vec.push(40);
   int_vec.push(50);

   println!("Vector elements: \n{:?}",int_vec);
}

Output:

Vector elements: 
[10, 20, 30, 40, 50]

Explanation:

Here, we created a vector int_vec that can store only integer elements. Then We stored some more elements using the push() function. After that, we printed the vector elements.

Rust Vectors Programs »






Comments and Discussions!

Load comments ↻






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