Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 2, 2019 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/bfd44733d4451734cf29a03ee88aa517 to your computer and use it in GitHub Desktop.
Save rust-play/bfd44733d4451734cf29a03ee88aa517 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
extern crate regex;
use regex::Regex;
fn main() {
let rg = Regex::new(r"(\d)-(\d)(\d)(\d)-(\d)(\d)(\d)(\d)(\d)-(\d)").unwrap();
// Regex::captures returns Option<Captures>,
// first element is the full match and others
// are capture groups
let isbn : Vec<_> = rg.captures("3-598-21508-8").unwrap().iter().skip(1).map(|x| x.unwrap().as_str().parse::<u64>().unwrap()).collect();
println!("{:?}", isbn);
// (x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0
print!("{}", (isbn[0] * 10 + isbn[1] * 9 + isbn[2] * 8 + isbn[3] * 7 + isbn[4] * 6 + isbn[5] * 5 + isbn[6] * 4 + isbn[7] * 3 + isbn[8] * 2 + isbn[9] * 1) % 11 == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment