Rust program to delete the given item from the array

Rust | Array Example: Write a program to delete the given item from the array.
Submitted by Nidhi, on October 22, 2021

Problem Solution:

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

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.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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