| Crates.io | country-emoji |
| lib.rs | country-emoji |
| version | 0.3.2 |
| created_at | 2020-10-26 07:35:11.736708+00 |
| updated_at | 2025-09-21 20:36:53.019695+00 |
| description | Converts between country names, ISO 3166-1 codes and flag emojis. |
| homepage | |
| repository | https://github.com/leodutra/country-emoji |
| max_upload_size | |
| id | 305514 |
| size | 91,117 |
A lightweight, fast Rust library that converts between country names, ISO 3166-1 codes and flag emojis. Features intelligent fuzzy matching, normalization, and comprehensive country data.
use country_emoji::{flag, code, name};
// Generate flag emoji from country code
assert_eq!(flag("US"), Some("๐บ๐ธ".to_string())); // โ ๐บ๐ธ
// Extract country code from flag emoji
assert_eq!(code("๐จ๐ฆ"), Some("CA")); // โ "CA"
// Get country name from code
assert_eq!(name("DE"), Some("Germany")); // โ "Germany"
// Convert country name to code
assert_eq!(code("Japan"), Some("JP")); // โ "JP"
```### Advanced Fuzzy Matching
The library handles many name variations and formats intelligently:
```rust
use country_emoji::code;
// Alternative names and abbreviations
assert_eq!(code("UK"), Some("GB")); // โ ๐ฌ๐ง
assert_eq!(code("UAE"), Some("AE")); // โ ๐ฆ๐ช
assert_eq!(code("Russia"), Some("RU")); // โ ๐ท๐บ
// Government titles and formal names
assert_eq!(code("Republic of Moldova"), Some("MD")); // โ ๐ฒ๐ฉ
assert_eq!(code("Democratic People's Republic of Korea"), Some("KP")); // โ ๐ฐ๐ต
assert_eq!(code("United States of America"), Some("US")); // โ ๐บ๐ธ
// Comma-reversed formats
assert_eq!(code("Virgin Islands, British"), Some("VG")); // โ ๐ป๐ฌ
assert_eq!(code("Korea, Republic of"), Some("KR")); // โ ๐ฐ๐ท
// Saint/St. normalization
assert_eq!(code("Saint Lucia"), Some("LC")); // โ ๐ฑ๐จ
assert_eq!(code("St. Lucia"), Some("LC"));
assert_eq!(code("St Lucia"), Some("LC"));
// And/ampersand equivalence
assert_eq!(code("Bosnia and Herzegovina"), Some("BA")); // โ ๐ง๐ฆ
assert_eq!(code("Bosnia & Herzegovina"), Some("BA"));
// Diacritic handling
assert_eq!(code("Cote d'Ivoire"), Some("CI")); // โ ๐จ๐ฎ
assert_eq!(code("Cรดte d'Ivoire"), Some("CI"));
// Partial matching for unique names
assert_eq!(code("Vatican"), Some("VA")); // โ ๐ป๐ฆ
For explicit conversions, use the direct API:
use country_emoji::{code_to_flag, flag_to_code, name_to_code, code_to_name, is_code, is_country_flag};
assert_eq!(code_to_flag("FR"), Some("๐ซ๐ท".to_string())); // โ ๐ซ๐ท
assert_eq!(flag_to_code("๐ฎ๐น"), Some("IT")); // โ "IT"
assert_eq!(name_to_code("Spain"), Some("ES")); // โ "ES"
assert_eq!(code_to_name("BR"), Some("Brazil")); // โ "Brazil"
assert!(is_code(Some("CA"))); // โ
Valid
assert!(is_country_flag("๐ฏ๐ต")); // โ
Valid
The library returns None for invalid or ambiguous inputs:
use country_emoji::code;
// Invalid inputs
assert_eq!(code("ZZ"), None); // โ Invalid
assert_eq!(code("Atlantis"), None); // โ Non-existent
// Ambiguous inputs (prevents false matches)
assert_eq!(code("Korea"), None); // โ Ambiguous (๐ฐ๐ต or ๐ฐ๐ท?)
assert_eq!(code("United"), None); // โ Too vague
This library is optimized for high performance:
cargo bench to see performance metricsTypical performance (release build):
The library includes comprehensive country data:
Add this to your Cargo.toml:
[dependencies]
country-emoji = "0.3"
Don't need Rust? Check out these alternatives:
This library builds upon excellent prior work:
country-emoji (JavaScript) - Original concept and API design
flag-emoji-from-country-code - Flag emoji generation technique
MIT @ Leo Dutra