Zipcodes
Zipcodes is a simple library for querying U.S. zipcodes. The full dataset is
embedded into the library at compile time (via include_bytes!) and lazily
decompressed on first access — no files, no network, no database.
This crate and the zipcodes Python package
are built from the same source: https://github.com/seanpianka/zipcodes.
The zipcode data is refreshed automatically every month by CI in the source repository.
The minimum supported Rust version (MSRV) is 1.82.
Installation
$ cargo add zipcodes
Quick Start
Example Output
Zipcode {
acceptable_cities: [],
active: true,
area_codes: [
"281",
"346",
"713",
"832",
],
city: "Cypress",
country: "US",
county: "Harris County",
lat: "29.9766",
long: "-95.6358",
state: "TX",
timezone: "America/Chicago",
unacceptable_cities: [],
world_region: "NA",
zip_code: "77429",
zip_code_type: "STANDARD",
}
Examples
Validating a Zipcode
use is_real;
Prefix and Substring Search
// All zipcodes starting with "1018"
let zips = similar_to;
// All zipcodes containing "018" anywhere
let zips = contains;
Advanced Filtering
The filter_by() function allows for powerful, custom queries using a vector
of closures.
use ;
Geographic Queries
// All zipcodes within 5 miles of Old Saybrook, CT.
let nearby = filter_by_coordinates;
// Great-circle distance in miles between two (lon, lat) points.
let miles = haversine;
Listing All Zipcodes
// Borrow the database without copying it...
let all = database;
// ...or get an owned copy.
let all_owned = list_all;
println!;
Zipcode Data
The zipcode data is embedded directly into the library at compile time via
include_bytes! and decompressed lazily on first use. This ensures fast
lookups at runtime without needing to read from a file or an external
database.
The dataset is assembled from unitedstateszipcodes.org (base data), the USPS ZIP Locale Detail file (active delivery ZIPs, public domain), and GeoNames postal data (GPS coordinates, licensed CC BY 4.0). It is refreshed monthly by an automated workflow in the source repository.
Contributing
Have an idea for a new feature? Feel free to open a pull request at https://github.com/seanpianka/zipcodes and contribute!