ADVERTISEMENT
ADVERTISEMENT

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 »



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.