You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用awc 2.0.3的Connector.rustls方法时遭遇rustls::client::ClientConfig与ClientConfig类型不匹配错误的技术咨询

解决awc Connector.rustls()的类型不匹配问题

这个错误的核心原因是rustls版本冲突:你手动引入的rustls = "0.20.1"和awc 2.0.3(以及它依赖的actix-http 2.2.1)所依赖的rustls版本不兼容。不同版本的rustls会生成不同的ClientConfig类型——哪怕它们的路径和名字看起来完全一样,编译器也会把它们当成毫无关系的类型,这就是你看到类型不匹配提示的根本原因。

第一步:修复依赖版本

首先,移除你手动添加的rustls = "0.20.1"依赖,因为actix-http 2.2.1的rustls特性已经自带了兼容版本的rustls(对应0.19.x系列)。修改后的Cargo.toml如下:

[dependencies]
awc = "2.0.3"
actix-web = "3.3.2"
futures-util = "0.3.17"
actix-http = { version = "2.2.1", features = ["rustls"] }

如果确实需要显式指定rustls版本,先运行cargo tree -p rustls查看项目中实际生效的rustls版本,再在Cargo.toml中指定该版本,确保和awc/actix-http的依赖完全对齐。

第二步:调整代码以适配正确类型

接下来修改代码,确保使用的是和awc兼容的rustls类型,同时注意rustls()方法要求参数是Arc<ClientConfig>:

use rustls::client::ClientConfig;
use awc::{Client, ClientBuilder, Connector};
use std::sync::Arc;

async fn get_client_config() -> ClientConfig {
    let root_certs = rustls::RootCertStore::empty();
    ClientConfig::builder()
        .with_safe_defaults()
        .with_root_certificates(root_certs)
        .with_no_client_auth()
}

async fn get_client() -> Client {
    let client_config = get_client_config().await;
    // 用Arc包裹ClientConfig,匹配awc方法的参数要求
    let connector = Connector::new().rustls(Arc::new(client_config)).finish();
    ClientBuilder::new().connector(connector).finish()
}

验证修复

修改完成后,运行cargo check或cargo build验证错误是否消失。如果仍有问题,执行cargo update确保所有依赖都更新到兼容版本。

内容的提问来源于stack exchange,提问作者GrvTyagi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 02:23:12