ADVERTISEMENT
ADVERTISEMENT

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 »



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.