Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Country Code

Country Code is the ISO 3166-1 alpha-2 two letter code that identifies a country, dependent territory, or special area of geographical interest. This crate’s CountryCode type is a validated, allocation free representation of it.

use ftracker_identifiers::CountryCode;

let code = CountryCode::parse("US").unwrap();
assert_eq!(code.as_str(), "US");
assert_eq!(code.as_bytes(), b"US");

If you hold a CountryCode, it is guaranteed to be a code that ISO 3166-1 officially assigns. There is no partially validated or “trust me” state.

Membership, not checksum

A country code has no check digit. It is valid exactly when its two letters name a code that the standard officially assigns, so this crate embeds that set as a compile time bitmap and tests membership against it. Only the codes are embedded. The country name, the alpha-3 code, and the numeric code are all out of scope, so CountryCode tells you whether a code is assigned, not what country it names.

In this chapter

  • Structure & Formats: the two letters of a country code and what is (and is not) modeled.
  • Parsing & Validation: what CountryCode::parse accepts, and the rules every constructor enforces.
  • Formatting & Display: rendering the canonical form without allocating.
  • Error Handling: the CountryCodeError variants and how to match on them.
  • Feature Flags: optional serde, schemars, arbitrary, and proptest integrations.
  • Examples: end to end usage, including sorting, deduplication, and use as a map or set key.

API reference

This book explains how and why to use CountryCode. For the full, generated API reference run:

cargo doc --open --all-features