How to find the index of elements in a vector based on the substring in the elements in Rust

1 Answer

0 votes
fn main() {
    let a_vec = vec!["bbc", "nnd", "wbb", "ccb", "qqd", "bbbbbo"];
    let mut index_vec = Vec::new();

    for (i, s) in a_vec.iter().enumerate() {
        if s.contains("bb") {
            index_vec.push(i);
        }
    }

    println!("{:?}", index_vec);
}

  
  
  
/*
run:
  
[0, 2, 5]
  
*/

 



answered Aug 18, 2024 by avibootz
...