12 个稳定版本
2.4.0 | 2022年8月30日 |
---|---|
2.3.0 | 2022年8月30日 |
2.2.0 | 2022年3月29日 |
2.1.0 | 2021年8月2日 |
1.0.5 | 2019年11月26日 |
在 国际化(i18n) 中排名 33
每月下载量 10,981
用于 7 个包(6 个直接使用)
175KB
3.5K SLoC
Celes
用于处理 ISO 3166-1 的便捷包。也兼容 no-std
环境。
如果缺少任何国家,请告诉我或提交 PR
主要结构是 Country
,它提供了以下属性
code
- 国家的三位数代码value
- 作为整数的代码alpha2
- 国家的 alpha2 字母集alpha3
- 国家的 alpha3 字母集long_name
- 国家的官方州名aliases
- 国家其他名称。例如,
俄罗斯联邦也称为俄罗斯,大不列颠及北爱尔兰联合王国也称为英格兰、大不列颠、北爱尔兰、苏格兰和联合王国。
可以使用蛇形命名的国家名称函数实例化每个国家
用法
use celes::Country;
fn main() {
let gb = Country::the_united_kingdom_of_great_britain_and_northern_ireland();
println!("{}", gb);
let usa = Country::the_united_states_of_america();
println!("{}", usa);
}
此外,每个国家都可以从字符串或其数值代码创建。 Country
提供了多种从方法来从字符串实例化它
from_code
- 从三位数代码创建Country
from_alpha2
- 从两位数代码创建Country
from_alpha3
- 从三位数代码创建Country
from_alias
- 从常见别名创建Country
。这仅适用于某些国家,因为并非所有国家都有别名from_name
- 从全州名(无空格或下划线)创建Country
Country
实现了 core::str::FromStr 特性,它接受之前提到的函数的有效任何参数,例如
- 国家别名,如 UnitedKingdom、GreatBritain、Russia、America
- 完整国家名称
- alpha2代码
- alpha3代码
如果您不确定使用哪个功能,只需使用 Country::from_str
即可,因为它接受任何有效的字符串值。Country::from_str
不区分大小写
字符串示例
use celes::Country;
use core::str::FromStr;
fn main() {
// All three of these are equivalent
let usa_1 = Country::from_str("USA").unwrap();
let usa_2 = Country::from_str("US").unwrap();
let usa_3 = Country::from_str("America").unwrap();
// All three of these are equivalent
let gb_1 = Country::from_str("England").unwrap();
let gb_2 = Country::from_str("gb").unwrap();
let gb_3 = Country::from_str("Scotland").unwrap();
}
许可证
许可下
贡献
除非您明确声明,否则您有意提交给作品以包含在内的任何贡献,根据Apache-2.0许可证定义,应按上述方式许可,不附加任何额外条款或条件。
依赖项
~0.4–1MB
~23K SLoC