Most best rust idiom for filter predicate

i have sample code, which example 1 - 4, where most representing rustlang style and if all four example have same result for performance output or not?
can some one can give me advice for write better code in rustlang.

 fn hashmap_test(){
 
     let mut score = HashMap::new();
 
     println!("Insert random score.");
     for i in 1..10 { score.insert(format!("Team_{}", i), rand::thread_rng().gen_range(0, 101)); }
 
     // example 1
     println!("\nExample filter 1.");
     for (key, value) in score.iter().filter(| cltn | *cltn.1 >= 50) {
         println!("score : {} is {}", key, value);
     }
 
     // example 2
     println!("\nExample filter 2.");
     for (key, value) in score.iter().filter(| cltn | cltn.1 >= &50) {
         println!("score : {} is {}", key, value);
     }
 
     // example 3
     println!("\nExample filter 3.");
     for (key, value) in score.iter().filter(| (key , val) | **val >= 50) {
         println!("score : {} is {}", key, value);
     }
 
     // example 4
     println!("\nExample filter 4.");
     for (key, value) in score.iter().filter(| (key , val) | val >= &&50) {
         println!("score : {} is {}", key, value);
     }
 
 }

I’d probably write it like this, but it mostly comes down to personal style— I strongly suspect that all of these will produce the same machine code when compiled.

    for (key, value) in score.iter().filter(|&(_, &val)| val >= 50) {
        println!("score : {} is {}", key, value);
    }

(Playground)

3 Likes

thanks for advice, it's new to me.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.