Avatar ·

How to convert a vector to HashSet in Rust: complete guide

Hello everyone! I recently started learning Rust and encountered a problem. I have a vector with numbers, and I want to get a HashSet from it to remove duplicates and quickly check for element existence. In other languages, like Python, this is done in one line: `set(my_list)`. But in Rust, I can't figure out how to do it correctly. Here's my code: ```rust let numbers = vec![1, 2, 3, 2, 1, 4, 5]; // Want to get a HashSet from this vector let unique_numbers: HashSet = numbers.???; // What to write here? ``` I tried using `numbers.into_iter().collect()`, but the compiler complains that it cannot infer the type. I tried explicitly specifying `HashSet::from_iter(numbers)`, but that didn't work either – it says the `FromIterator` trait is not implemented. I also heard about the `drain()` method, but I'm not sure.