pub struct Language { /* private fields */ }Expand description
A compact, zero-allocation language tag.
Parses the language [-script] [-region] prefix of a BCP 47 language tag.
This type captures only the language + optional script + optional region subtags from a
BCP 47 language tag. Parsing is case-insensitive; output is normalized to:
- language: 2–3 lowercase letters (e.g.
EN→en) - script: 4 titlecase letters (e.g.
LATN→Latn) - region: 2 uppercase letters or 3 digits (e.g.
us→US,419→419)
Trailing subtags (variants, extensions, private use) are validated for basic structural
conformance but discarded. Extended language subtags (extlang) are not supported and will
error.
Implementations§
Source§impl Language
impl Language
Sourcepub const MAX_LEN: usize = 12
pub const MAX_LEN: usize = 12
The maximum length of the canonical language[-Script][-REGION] form.
Sourcepub fn parse(s: &str) -> Result<Language, ParseLanguageError>
pub fn parse(s: &str) -> Result<Language, ParseLanguageError>
Parses a language tag, keeping only language/script/region.
Sourcepub fn parse_prefix(s: &str) -> Result<(Language, &str), ParseLanguageError>
pub fn parse_prefix(s: &str) -> Result<(Language, &str), ParseLanguageError>
Parses the language[-Script][-REGION] prefix of a tag, returning the remainder.
The remainder starts at the first unconsumed subtag (without a leading -/_). This can
be used to inspect or process variants, extensions, or private-use subtags without forcing
this type to model them.
use parlance::Language;
let (lang, rest) = Language::parse_prefix("tr-Latin-TR").unwrap();
assert_eq!(lang.as_str(), "tr");
assert_eq!(rest, "Latin-TR");