未声明的结构体用法

问题描述 投票:0回答:1

使用 reqwest,我尝试将响应转换为结构。我正在遵循此site中的示例。

我的文件:twilio.rs,我有以下代码

//use serde::{Serialize, Deserialize};
use serde_json::json;
use serde::Deserialize;
use serde::Serialize;

#[derive(Debug, Serialize, Deserialize)]
pub struct TwilioResponse {
    body: String,
    ...
}

pub mod twilio_service{

    pub struct Twilio {
        url: String,
        ...
    }



    impl Twilio {
        pub fn new(url: String, phone_number: String, account_sid: String, auth_token: String, message: String, send_to: String) -> Twilio {
            Twilio {
                url,
                phone_number,
                account_sid,
                auth_token,
                message,
                send_to
            }
        }

        pub async fn send(&self) {

            let url = format!("{}{}{}", self.url, self.account_sid, "/Messages.json");

            let client = reqwest::Client::new();
            let res = client.post(url)
                .basic_auth(&self.account_sid, Some(&self.auth_token))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .form(&[("From", &self.phone_number), ("To", &self.send_to), ("Body", &self.message)])
                .send()
                .await
                .unwrap();


            let text_response = res.text().await.unwrap();
            let json: twilio::TwilioResponse = serde_json::from_str(&text_response).unwrap();


        }
    }   
 }

我遇到的问题是当我编译应用程序时,出现以下错误:

let json: twilio::TwilioResponse = serde_json::from_str(&text_response).unwrap();
                   ^^^^^^ use of undeclared crate or module `twilio`

如果我将代码修改为:

let json: TwilioResponse = serde_json::from_str(&text_response).unwrap();

然后我收到错误

let json: TwilioResponse = serde_json::from_str(&text_response).unwrap();
^^^^^^^^^^^^^^ not found in this scope

这是我第一次尝试 Rust,需要建议。谢谢

rust rust-cargo
1个回答
0
投票

您第一次尝试的问题是,使用中的裸

twilio
指的是当前模块的板条箱或子模块或某些其他导入的符号,两者都不存在。

根据您声明

twilio
模块的位置,有多种导入方法。

  1. 由于
    twilio_service
    twilio
    的子模块,因此您可以使用
    super
    关键字,这意味着该模块之上的模块:
    use super::TwilioResponse;
    
  2. 如果
    twilio
    直接在您的
    lib.rs
    main.rs
    中声明,您可以使用
    crate
    来引用 crate 根并指定从那里开始的完整路径:
    use crate::possible_more_modules::twilio::TwilioResponse;
    
© www.soinside.com 2019 - 2024. All rights reserved.