The source code to check an item contains in HashSet or not is given below. The given program is compiled and executed successfully.
// Rust program to check an item contains
// in HashSet or not
use std::collections::HashSet;
use std::io;
fn main() {
let mut set:HashSet<i32> = HashSet::new();
let mut item:i32=0;
let mut input = String::new();
set.insert(10);
set.insert(20);
set.insert(30);
set.insert(40);
set.insert(50);
println!("HashSet: \n{:?}",set);
println!("Enter Item: ");
io::stdin().read_line(&mut input).expect("Not a valid string");
item = input.trim().parse().expect("Not a valid number");
if(set.contains(&item))
{
println!("Item {} is available in HashSet",item);
}
else
{
println!("Item {} is not available in HashSet",item);
}
}
Output:
HashSet:
{20, 40, 50, 30, 10}
Enter Item:
30
Item 30 is available in HashSet
Explanation:
Here, we created a HashSet to store integer elements. Then we added items using the insert() method. After that, we read an item from the user and check an item exists in HashSet or not, and print the appropriate message.
Program/Source Code:
The source code to check an item contains in HashSet or not is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here, we created a HashSet to store integer elements. Then we added items using the insert() method. After that, we read an item from the user and check an item exists in HashSet or not, and print the appropriate message.
need an explanation for this answer? contact us directly to get an explanation for this answer