三行代码  ›  专栏  ›  技术社区  ›  Jika

在Rust中使用if语句时的不同函数类型

  •  1
  • Jika  · 技术社区  · 2 月前

    use isahc::{
        HttpClient, 
        config::{
            RedirectPolicy, 
            VersionNegotiation,
            SslOption}, 
        prelude::*
    };
    
    use std::{
        time::Duration
    };
    
    pub struct http {
        pub timeout: u64    
    }
    
    
    
    
    
    impl http {
        pub fn send(&self) -> HttpClient  {
            let client = 
                HttpClient::builder()
                    .version_negotiation(VersionNegotiation::http11())
                    .redirect_policy(RedirectPolicy::None)
                    .timeout(Duration::from_secs(self.timeout));
                    .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS);
            return client.build().unwrap();
        }
    
        }
    
    fn main(){
        let req = http{ timeout:"20".parse().unwrap()};
        let test = req.send();
        test.get("https://www.google.com");
    }
    

    if statement 在这些选项上,所以我尝试在这种情况下使用它,但我总是得到不同的函数返回类型

    impl http {
        pub fn send(&self) -> HttpClient  {
            let client = 
                HttpClient::builder()
                    .version_negotiation(VersionNegotiation::http11())
                    .redirect_policy(RedirectPolicy::None)
                    .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS);
            if 1 == 1 {
                client.timeout(Duration::from_secs(self.timeout));
            }
            return client.build().unwrap();
        }
    
        }
    
    • 货物产量
    warning: type `http` should have an upper camel case name
      --> src/sender.rs:14:12
       |
    14 | pub struct http {
       |            ^^^^ help: convert the identifier to upper camel case: `Http`
       |
       = note: `#[warn(non_camel_case_types)]` on by default
    
    error[E0382]: use of moved value: `client`
      --> src/sender.rs:32:16
       |
    24 |         let client = 
       |             ------ move occurs because `client` has type `HttpClientBuilder`, which does not implement the `Copy` trait
    ...
    30 |             client.timeout(Duration::from_secs(self.timeout));
       |             ------ value moved here
    31 |         }
    32 |         return client.build().unwrap();
       |                ^^^^^^ value used here after move
    
    

    我试图更改函数类型异常,但无法使用该类的函数 client.get() 例如

    options : dict = {
      "redirects": False,
      "ssl_path":"~/cer.pem"
    }
    
    
    def send(opts):
       # not real httplib ! 
       r = httplib.get("http://stackoverflow.com")
       if opts.get('redirects') == True:
           r.redirects = True
       if opts.get('cert_path',''):
           r.ssl = opts.get('cert_path')
       return r.send()
    
    def main():
       send(options)
    

    谢谢

    1 回复  |  直到 2 月前
        1
  •  1
  •   TenderShortGoldenRetriever    2 月前

    因为这是一个构建器模式,所以每个函数调用都会使用构建器并将其返回。所以你需要捕捉 client timeout 函数以继续使用它。注意,您还需要 客户 易变的。

    差不多

    impl http {
        pub fn send(&self) -> HttpClient  {
            let mut client = 
                HttpClient::builder()
                    .version_negotiation(VersionNegotiation::http11())
                    .redirect_policy(RedirectPolicy::None)
                    .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS);
            if 1 == 1 {
                client = client.timeout(Duration::from_secs(self.timeout));
            }
            return client.build().unwrap();
        }
    }