Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
maxminddb rust,获取特定语言的国家值_Rust - Fatal编程技术网

maxminddb rust,获取特定语言的国家值

maxminddb rust,获取特定语言的国家值,rust,Rust,我决定开始学习Rust,我还没有完成他们的书,但我正在尝试构建和运行其他项目,以便从源代码中学习。我现在对maxmind rust crate感兴趣,特别是我想从.mmdb文件中检索国家、城市和asn值 我试图将structmaxmind::geoip2::Country转换为字符串,并使用json板条箱,但导致了无法自行修复的错误 使用的代码: 使用maxminddb::geoip2; 使用std::net::IpAddr; 使用std::str::FromStr; fn main() { 让

我决定开始学习Rust,我还没有完成他们的书,但我正在尝试构建和运行其他项目,以便从源代码中学习。我现在对maxmind rust crate感兴趣,特别是我想从.mmdb文件中检索国家、城市和asn值

我试图将struct
maxmind::geoip2::Country
转换为字符串,并使用json板条箱,但导致了无法自行修复的错误

使用的代码:

使用maxminddb::geoip2;
使用std::net::IpAddr;
使用std::str::FromStr;
fn main()
{
让mmdb_file=maxminddb::Reader::open(“C:\\path\\to\\GeoLite2 City.mmdb”).unwrap();
让ip_addr:IpAddr=FromStr::FromStr(“8.8.8.8”).unwrap();
让geoip2_country:geoip2::country=mmdb_file.lookup(ip_addr).unwrap();
println!(“{:?}”,geoip2_国家);
}
输出为:

Country
{
    continent: Some(Continent
    {
        code: Some("NA"),
        geoname_id: Some(6255149),
        names: Some(
        {
            "de": "Nordamerika",
            "en": "North America",
            "es": "Norteam?rica",
            "fr": "Am?rique du Nord",
            "ja": "?????",
            "pt-BR": "Am?rica do Norte",
            "ru": "???????? ???????",
            "zh-CN": "???"
        })
    }),
    country: Some(Country
    {
        geoname_id: Some(6252001),
        iso_code: Some("US"),
        names: Some(
        {
            "de": "USA",
            "en": "United States",
            "es": "Estados Unidos",
            "fr": "?tats-Unis",
            "ja": "???????",
            "pt-BR": "Estados Unidos",
            "ru": "???",
            "zh-CN": "??"
        })
    }),
    registered_country: Some(Country
    {
        geoname_id: Some(6252001),
        iso_code: Some("US"),
        names: Some(
        {
            "de": "USA",
            "en": "United States",
            "es": "Estados Unidos",
            "fr": "?tats-Unis",
            "ja": "???????",
            "pt-BR": "Estados Unidos",
            "ru": "???",
            "zh-CN": "??"
        })
    }),
    represented_country: None,
    traits: None
}
哪个是maxminddb::geoip2::Country结构()

将最后一行代码更改为

println!(“{:?}”,geoip2_country.country);
仅输出
国家/地区
字段:

country: Some(Country
{
    geoname_id: Some(6252001),
    iso_code: Some("US"),
    names: Some(
    {
        "de": "USA",
        "en": "United States",
        "es": "Estados Unidos",
        "fr": "?tats-Unis",
        "ja": "???????",
        "pt-BR": "Estados Unidos",
        "ru": "???",
        "zh-CN": "??"
    })
}),

但是看看maxminddb::geoip2::model::Country()的结构,如果我想获得“en”的国家名称,我很困惑如何从这个结构及其对(语言代码、国家名称)中检索键。

您可以看看测试是如何编写的,它会让您了解如何提取您要查找的信息:

(事实上,在查看测试后,我没有发现任何示例提取您要查找的内容)

这应该会返回您期望的结果:

let country_name = geoip2_country.country
                    .and_then(|cy| cy.names)
                    .and_then(|n| n.get("en")
                    .map(String::from));

您可以看看测试是如何编写的,它会让您了解如何提取您要查找的信息:

(事实上,在查看测试后,我没有发现任何示例提取您要查找的内容)

这应该会返回您期望的结果:

let country_name = geoip2_country.country
                    .and_then(|cy| cy.names)
                    .and_then(|n| n.get("en")
                    .map(String::from));