fn invalid_isbn_with_invalid_X() { assert!(!is_valid_isbn("3-598-2X507-9")); }
just a title describe, whey X in the middle of ISBn string is not valid?
below is my code passed the exercise
pub fn is_valid_isbn(isbn: &str) -> bool {
let mut cnt = 10;
let mut sum = 0;
for ch in isbn.chars() {
match ch {
'0'..='9' => {
let n = ch.to_digit(10).unwrap_or(0);
sum += n * cnt;
if cnt < 1 {
return false;
}
cnt = cnt - 1;
}
'X' => {
if cnt == 1 {
sum += 10 * cnt;
if cnt < 1 {
return false;
}
cnt = cnt - 1;
} else {
return false;
}
}
_ => continue,
}
}
sum % 11 == 0 && cnt == 0
}