Avatar ·

How to iterate over Vec<T> with getting element index in Rust?

Hello everyone! I recently started learning Rust, and I encountered a problem with traversing a vector while accessing the element index. In other languages, like Python or JavaScript, I'm used to writing something like `for i, value in enumerate(list)` or `for (let i = 0; i < arr.length; i++)`. In Rust, I know I can simply use `for value in vec`, but then I lose the index. I need to print all elements of the vector along with their position, for example: ```rust let items = vec!["apple", "banana", "cherry"]; // I want to get: "0: apple", "1: banana", "2: cherry" ``` I tried doing this: ```rust for i in 0..items.len() { println!("{}: {}", i, items[i]); } ``` This works, but I've heard that accessing by index using square brackets can cause a panic if the index goes out of bounds.