1pub fn cpf(cpf: &str) -> bool {
2 let cpf: Vec<u8> = cpf
3 .chars()
4 .filter_map(|c| c.to_digit(10))
5 .map(|d| d as u8)
6 .collect();
7
8 if cpf.len() != 11 || cpf.iter().all(|&d| d == cpf[0]) {
9 return false;
10 }
11
12 let mut sum = 0;
13 for i in 0..9 {
14 sum += (cpf[i] as usize) * (10 - i);
15 }
16 let rest = (sum * 10) % 11;
17 let first_digit = if rest == 10 { 0 } else { rest as u8 };
18
19 sum = 0;
20 for i in 0..10 {
21 sum += (cpf[i] as usize) * (11 - i);
22 }
23 let rest = (sum * 10) % 11;
24 let second_digit = if rest == 10 { 0 } else { rest as u8 };
25
26 first_digit == cpf[9] && second_digit == cpf[10]
27}