Q:

Rust program to delete the given item from the array

belongs to collection: Rust Arrays Programs

0

In this program, we will create an array of integers then we will delete the given item from the array.

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 delete the given item from the array is given below. The given program is compiled and executed successfully.

// Rust program to delete the given 
// item from the array

use std::io;

fn main() 
{
	let mut arr:[usize;6] = [10,20,30,40,50,60];
	let mut i:usize=0;
	let mut j:usize=0;
	let mut item:usize=0;
	let mut flag:isize=0;

	let mut input = String::new();
	println!("Enter Item: ");
	io::stdin().read_line(&mut input).expect("Not a valid string");
	item = input.trim().parse().expect("Not a valid number");

	while i<=5
	{
		if arr[i] == item 
		{
			flag = 1;
			j=i;
			while j<=4
			{
				arr[j] = arr[j+1];
				j=j+1;
			}
			break;
		}
		i=i+1;
	}

	if flag == 1 
	{
		println!("\nItem {} deleted successfully.", item);

		println!("\nArray elements after deletion:");
		i=0;
		while i<=4
		{
			print!("{} ", arr[i]);
			i=i+1;
		}
	} 
	else 
	{
		println!("\n{} not found.", item);
	}
}

Output:

Enter Item: 
50

Item 50 deleted successfully.

Array elements after deletion:
10 20 30 40 60

Explanation:

Here, we created an array of integers with 6 elements, and then we deleted the given item from the array and print the updated array.

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

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Rust program to delete the given item from the arr...