Hi, I'm new to the Rust language and I'm doing a simple local search algorithm for a project. I need to iterate over a vector and generate neighbors until I find a better one or reach the end. In C++, a for loop with a stop condition would fit for me, but since Rust for loops are differente I don't know if there is a similar tool I could use. I'm sure the answer is simple, but I haven't found it yet.
You can exit the for loop early using break. But instead of copying the code as-is, try to leverage Rust's iterators to do the job in a more natural way. For example, you can use find to find the first element matching the condition:
if let Some(element) = vec.iter().find(|x| x > a) {
//...
}