Crates.io | phonenumber |
lib.rs | phonenumber |
version | 0.3.6+8.13.36 |
source | src |
created_at | 2017-07-31 10:23:48.600803 |
updated_at | 2024-07-09 13:30:10.80515 |
description | Library for parsing, formatting and validating international phone numbers. |
homepage | |
repository | https://github.com/whisperfish/rust-phonenumber |
max_upload_size | |
id | 25790 |
size | 13,527,650 |
Rust version of libphonenumber. We currently require 1.58.0 as minimum supported Rust version (MSRV).
Add this to your Cargo.toml
:
[dependencies]
phonenumber = "0.3"
The following example parses, validates and formats the given phone number.
use phonenumber::Mode;
use std::env;
fn main() {
let mut args = env::args().skip(1).collect::<Vec<_>>();
if args.len() < 1 {
panic!("not enough arguments");
}
let number = args.pop().unwrap();
let country = args.pop().map(|c| c.parse().unwrap());
let number = phonenumber::parse(country, number).unwrap();
let valid = phonenumber::is_valid(&number);
if valid {
println!("\x1b[32m{:#?}\x1b[0m", number);
println!();
println!("International: {}", number.format().mode(Mode::International));
println!(" National: {}", number.format().mode(Mode::National));
println!(" RFC3966: {}", number.format().mode(Mode::Rfc3966));
println!(" E.164: {}", number.format().mode(Mode::E164));
}
else {
println!("\x1b[31m{:#?}\x1b[0m", number);
}
}