Q:

Rust program to create a vector that can store only integers

belongs to collection: Rust Vectors Programs

0

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.

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Rust program to iterate the items of the vector us...