Expand description
§MaxMind DB Reader
This library reads the MaxMind DB format, including the GeoIP2 and GeoLite2 databases.
§Features
This crate provides several optional features for performance and functionality:
mmap(default: disabled): Enable memory-mapped file access for better performance in long-running applicationssimdutf8(default: disabled): Use SIMD instructions for faster UTF-8 validation during string decodingunsafe-str-decode(default: disabled): Skip UTF-8 validation entirely for maximum performance (~20% faster lookups)
Note: simdutf8 and unsafe-str-decode are mutually exclusive.
§Database Compatibility
This library supports all MaxMind DB format databases:
- GeoIP2 databases (City, Country, Enterprise, ISP, etc.)
- GeoLite2 databases (free versions)
- Custom MaxMind DB format databases
§Thread Safety
The Reader is Send and Sync, making it safe to share across threads.
This makes it ideal for web servers and other concurrent applications.
§Quick Start
use maxminddb::{Reader, geoip2};
use std::net::IpAddr;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Open database file
let reader = Reader::open_readfile("/path/to/GeoIP2-City.mmdb")?;
// Look up an IP address
let ip: IpAddr = "89.160.20.128".parse()?;
let result = reader.lookup(ip)?;
if let Some(city) = result.decode::<geoip2::City>()? {
// Access nested structs directly - no Option unwrapping needed
println!("Country: {}", city.country.iso_code.unwrap_or("Unknown"));
}
Ok(())
}§Selective Field Access
Use decode_path to extract specific fields without deserializing the entire record:
use maxminddb::{Reader, PathElement};
use std::net::IpAddr;
let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = "89.160.20.128".parse().unwrap();
let result = reader.lookup(ip).unwrap();
let country_code: Option<String> = result.decode_path(&[
PathElement::Key("country"),
PathElement::Key("iso_code"),
]).unwrap();
println!("Country: {:?}", country_code);Modules§
- geoip2
- GeoIP2 and GeoLite2 database record structures
Macros§
- path
- Creates a path for use with
LookupResult::decode_path().
Structs§
- Lookup
Result - The result of looking up an IP address in a MaxMind DB.
- Metadata
- Metadata about the MaxMind DB file.
- Reader
- A reader for the MaxMind DB format. The lifetime
'datais tied to the lifetime of the underlying buffer holding the contents of the database file. - Within
- Iterator over IP networks within a CIDR range.
- Within
Options - Options for network iteration.
Enums§
- MaxMind
DbError - Error returned by MaxMind DB operations.
- Path
Element - A path element for navigating into nested data structures.