1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pub fn validate(value: &str) -> bool {
let mut chars: Vec<char> = value
.chars()
.collect();
chars.reverse();
let mut is_second = false;
let mut sum = 0;
for c in chars {
assert!(c.is_digit(10));
let mut d = c.to_digit(10).unwrap();
if is_second {
d *= 2;
}
sum += d / 10;
sum += d % 10;
is_second = !is_second;
}
sum % 10 == 0
}
#[cfg(test)]
mod tests {
use super::validate;
#[test]
fn test_pass() {
assert!(validate("6796265520244"));
assert!(validate("4844161459546174"));
assert!(validate("4035300539804083"));
assert!(validate("378868637988407"));
assert!(validate("4111111111111111"))
}
#[test]
fn test_fail() {
assert!(!validate("6796265520247"));
assert!(!validate("4844161459546175"));
assert!(!validate("4035300539804082"));
assert!(!validate("378868637988406"));
}
}