Avatar ·

How to find the index of an element in an array, vector, or slice in Rust?

Hello everyone! I recently started learning Rust and encountered a seemingly simple task: I need to find the index of an element in an array or vector. In other languages, like Python or JavaScript, this is done with a single built-in function like `.index()` or `.findIndex()`. But in Rust, I can't find anything similar. Here's an example: I have a vector of numbers, and I want to find the index of the number 42. ```rust let numbers = vec![10, 20, 30, 42, 50]; // How to get the index of 42? I expected something like numbers.index_of(42) ``` I tried using `.iter().position()`, but I didn't understand how it works and what it returns. I also tried writing a loop with `enumerate`, but it looks cumbersome. Is there an idiomatic way in Rust to find the index of an element in an array, vector, or slice? I need a solution for