| Crates.io | luhn_tools |
| lib.rs | luhn_tools |
| version | 0.3.0 |
| created_at | 2024-12-14 00:12:03.690203+00 |
| updated_at | 2024-12-14 01:08:05.789719+00 |
| description | A fast, minimal implementation of the Luhn algorithm. |
| homepage | https://github.com/jrrembert/luhn_rs |
| repository | https://github.com/jrrembert/luhn_rs |
| max_upload_size | |
| id | 1482718 |
| size | 20,604 |
A fast, minimal implementation of the Luhn algorithm.
rand for random generation)Add one of these to your Cargo.toml:
# Option 1: Use defaults (includes `std`)
[dependencies]
luhn_algo = "0.3.0"
# Option 2: Include `random` feature
[dependencies]
luhn_algo = { version = "0.3.0", features = ["random"] }
use luhn_tools::{generate, validate, random, GenerateOptions};
// Generate a checksum and return new Luhn number
let result = generate("7992739871", None).unwrap();
assert_eq!(result, "79927398713");
// Generate only the checksum
let options = Some(GenerateOptions { checksum_only: true });
let checksum = generate("7992739871", options).unwrap();
assert_eq!(checksum, "3");
// Validate a Luhn number
assert!(validate("79927398713").unwrap());
// Generate a random valid Luhn number of length 10
let random_number = random("10").unwrap();
assert!(validate(&random_number).unwrap());
# view docs
$ cargo doc --no-deps --open
# run tests
$ cargo test --all-features
# run benchmarks
$ cargo bench
Contributions welcome! Please feel free to submit a Pull Request.
MIT