为什么我无法访问字段(类型上没有字段)

问题描述 投票:0回答:1
  1. 类型上无字段
    main
    impl Future<Output = Result<WeatherData, reqwest::Error>>
    未知领域[E0609]
#[derive(Debug, Deserialize, Clone)]
struct WeatherData {
    main: Main,
}

#[derive(Debug, Deserialize, Clone)]
struct Main {
    temp: Vec<f32>,
}

async fn get_weather() -> Result<WeatherData, reqwest::Error> {
    reqwest::get("https://api.openweathermap.org/data/2.5/weather?q=neftekamsk&units=metric&cnt=8&appid=apikeyhere")
        .await?
        .json::<WeatherData>()
        .await
}
fn test() {
    get_weather().main.temp
}

新用户尝试 Rust

rust
1个回答
0
投票

检查您的数据类型

async fn test() {
    get_weather().await.unwrap().main.temp
    //            ^     ^
    //            |     L the return type is Result<WeatherData, reqwest::Error>,
    //            |       need to unwrap before acccessing.
    //            |
    //            L get_weather() is async, you will need to .await for it to finish
}
© www.soinside.com 2019 - 2024. All rights reserved.