Why is my for loop code slower than an iterator?
问题 I am trying to solve the leetcode problem distribute-candies. It is easy, just find out the minimum between the candies' kinds and candies half number. Here's my solution (cost 48ms): use std::collections::HashSet; pub fn distribute_candies(candies: Vec<i32>) -> i32 { let sister_candies = (candies.len() / 2) as i32; let mut kind = 0; let mut candies_kinds = HashSet::new(); for candy in candies.into_iter() { if candies_kinds.insert(candy) { kind += 1; if kind > sister_candies { return sister