To add an item to a Set in Rust, you can use the standard library's HashSet data structure.
In this example,
my_set
with the type of items it will contain.insert
method to add an item to the set.use std::collections::HashSet;
fn main() {
let mut my_set = HashSet::new();
my_set.insert("item1");
println!("{:?}", my_set);
}
{"item1"}
In this example,
number_set
with the type of items it will contain.insert
method to add an integer to the set.use std::collections::HashSet;
fn main() {
let mut number_set = HashSet::new();
number_set.insert(123);
println!("{:?}", number_set);
}
{123}
In this tutorial, we learned How to add an Item to a Set in Rust language with well detailed examples.