Rust编译错误:Actix回退至不兼容的旧版本
问题:Actix依赖版本冲突导致编译错误
我重启了一个一年半前的小型Rust项目——这是个用于管理书评、书籍引用及关联关系的简易API,也是我第一个Rust项目。更新依赖后执行cargo build时遇到编译错误,错误显示Actix相关旧版本代码与当前依赖不兼容。
具体编译错误
error[E0277]: cannot subtract `TimeDelta` from `Tm` --> C:\Users\zerok\.cargo\registry\src\index.crates.io-6f17d22bba15001f\actix-http-1.0.1\src\cookie\jar.rs:224:44 | 224 | cookie.set_expires(time::now() - Duration::days(365)); | ^ no implementation for `Tm - TimeDelta` | = help: the trait `Sub<TimeDelta>` is not implemented for `Tm` = help: the following other types implement trait `Sub<Rhs>`: `Tm` implements `Sub<time::Duration>` `Tm` implements `Sub` error[E0308]: mismatched types --> C:\Users\zerok\.cargo\registry\src\index.crates.io-6f17d22bba15001f\actix-http-1.0.1\src\cookie\mod.rs:736:40 | 736 | self.set_expires(time::now() + twenty_years); | ^^^^^^^^^^^^ expected `Duration`, found `TimeDelta`
项目的Cargo.toml内容
[package] name = "rust_web_server" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diesel = { version = "2.0.3", features = ["sqlite", "r2d2", "returning_clauses_for_sqlite_3_35"] } dotenvy = "0.15" actix-web = "4" actix-web-httpauth = { git = "https://github.com/actix/actix-web-httpauth" } chrono = { version = "0.4.10", features = ["serde"] } derive_more = "0.99.2" futures = "0.3.1" r2d2 = "0.8.8" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" alcoholic_jwt = "1.0.0" reqwest = "0.9.22" actix-rt = "2.7.0" dotenv = "0.15.0" tokio-test = "0.4.2" actix-service = { version = "2.0.2" } [dependencies.uuid] version = "1.3.0" features = [ "v4", "fast-rng" ]
问题根源与解决步骤
问题根源
错误来自actix-http 1.0.1这个老旧版本,而你指定了actix-web = "4"(正常应依赖actix-http 3.x)。导致版本回退的核心原因是依赖冲突:
actix-web-httpauth使用git仓库直接拉取的方式,其依赖配置未正确适配actix-web 4,触发cargo自动选择兼容的旧版actix-http。reqwest = "0.9.22"版本过旧,它的依赖链可能引入过时包,间接干扰actix系列依赖的版本选择。
解决步骤
替换actix-web-httpauth为稳定适配版
去掉git依赖,改用官方适配actix-web 4的稳定版本:actix-web-httpauth = "0.8"更新reqwest到兼容版本
将过时的reqwest 0.9替换为适配当前生态的版本:reqwest = "0.11"注意:版本升级后可能需要调整代码中reqwest的调用逻辑,因为跨大版本API有变化。
固定actix-http版本
在Cargo.toml中明确指定actix-http版本,强制与actix-web 4匹配:actix-http = "3"清理并重新构建
执行命令清理旧依赖缓存,重新拉取并构建:cargo clean cargo update cargo build
内容的提问来源于stack exchange,提问作者Zerok
相关产品推荐
相关产品推荐

