Q:

Rust program to push or insert an item into vector

belongs to collection: Rust Vectors Programs

0

In this program, we will create a vector to store the name of countries then we will push the name of countries using the push() function.

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 push or insert an item into a vector is given below. The given program is compiled and executed successfully.

// Rust program to push or insert 
// an item into vector

fn main() {
   let mut countries = vec!["INDIA","USA"];
   
   countries.push("UK");
   countries.push("CANADA");
   countries.push("ENGLAND");
   
   println!("Countries are:\n{:?}", countries);
}

Output:

Countries are:
["INDIA", "USA", "UK", "CANADA", "ENGLAND"]

Explanation:

Here, we created a vector to store the name of countries, and then we added the items into the vector using the push() function. After that, we printed the elements of the vector.

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

total answers (1)

Rust program to perform the POP operation... >>
<< Rust program to find the length of the vector...