| Crates.io | phone_type |
| lib.rs | phone_type |
| version | 1.0.0-beta.1 |
| created_at | 2022-12-06 23:07:22.678821+00 |
| updated_at | 2025-09-20 06:20:22.240046+00 |
| description | Phone type for Rust |
| homepage | https://github.com/jonhteper/phone-type |
| repository | https://github.com/jonhteper/phone-type |
| max_upload_size | |
| id | 731464 |
| size | 54,502 |
A comprehensive Rust crate for handling phone numbers with advanced E.164 support and compile-time country code resolution.
Add in Cargo.toml:
phone_type = "1.0.0-beta.1"
Or run in your project directory:
cargo add phone_type
Use in structures:
use phone_type::*;
struct ContactInformation {
pub name: String,
pub age: i8,
pub phone: Phone,
}
fn main() {
let info = ContactInformation {
name: "John Doe".to_string(),
age: 33,
phone: Phone::build("111 111 1111").unwrap(),
};
/*...*/
}
Important: Enable e164 feature first.
use phone_type::Phone;
fn main() {
// Parse E.164 format numbers
let phone = Phone::from_e_164("+1234567890").unwrap();
println!("Country code: {:?}", phone.country_code()); // Some("1")
println!("National number: {}", phone.number()); // "234567890"
// Get country information
if let Some(info) = phone.country_info() {
println!("Country: {} ({})", info.name, info.iso_code); // "Canada (CA)"
}
// Format with separators
println!("Formatted: {}", phone.number_with_separator('-')); // "234-567-890"
}
use phone_type::Phone;
fn main() {
let examples = vec![
"+52155551234", // Mexico
"+4915123456789", // Germany
"+81312345678", // Japan
"+86138123456789", // China
];
for number in examples {
match Phone::from_e_164(number) {
Ok(phone) => {
if let Some(info) = phone.country_info() {
println!("{}: {} ({})",
number, info.name, info.iso_code);
}
}
Err(e) => println!("Invalid: {} - {:?}", number, e),
}
}
}
This crate provides the following features:
serde - Serialization/deserialization supporte164 - E.164 international phone number support with compile-time country codesImportant: No features are enabled by default. You must explicitly enable the features you need.
phone_type = { version = "1.0.0-beta.1", features = ["serde", "e164"] }
The E.164 functionality uses Perfect Hash Functions (PHF) to resolve country codes at compile time. This means:
// This lookup happens at compile time!
let phone = Phone::from_e_164("+1234567890").unwrap();
let country = phone.country_info().unwrap(); // Instant lookup
Serde support is available behind the serde feature:
use serde::{Serialize, Deserialize};
use serde_json::json;
use phone_type::*;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Contact {
pub name: String,
pub phone: Phone,
}
fn main() {
let contact_json = json!({
"name": "John Doe",
"phone": "111 111 1111"
});
let contact: Contact = serde_json::from_value(contact_json).unwrap();
println!("{:?}", contact);
}
The E.164 country codes are sourced from this list and compiled into the binary at build time, ensuring:
Run the included examples:
cargo run --example e164_demo --features e164
Contributions are welcome! The country code data can be updated by modifying data/country_codes.json and rebuilding.