any e all sugli iteratori in Rust
Livello 3292 battiture
all() restituisce true solo se la closure è vera per ogni elemento, mentre any() restituisce true se lo è per almeno uno. Entrambi si fermano appena la risposta è nota.
Codice Rust
1fn main() { // Cinque numeri pari2 let nums = vec![2, 4, 6, 8, 10];3 // all è true solo se ogni elemento è pari4 let all_even = nums.iter().all(|x| x % 2 == 0); // Stampa All even: true5 println!("All even: {}", all_even);6 // any è true appena un elemento vale 107 let has_ten = nums.iter().any(|x| *x == 10); // Stampa Has 10: true8 println!("Has 10: {}", has_ten);9 // Nessun elemento è dispari, quindi any restituisce false10 let has_odd = nums.iter().any(|x| x % 2 != 0); // Stampa Has odd: false11 println!("Has odd: {}", has_odd);12}Output
All even: true Has 10: true Has odd: false
Esercitati a digitare questo codice. I risultati non vengono registrati in classifica.
Simboli in questo codice
I simboli che digiti in questo codice Rust e quante volte compaiono.
(× 10)× 10=× 9,× 7;× 7_× 6.× 6"× 6|× 6!× 5