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

为何anyhow 1.0.66在Rust稳定版中无法正常编译?

问题:anyhow 1.0.66 在稳定版Rust下编译失败,提示需要nightly版本

创建全新Rust项目后,引入了anyhow 1.0.66依赖,安装了最新稳定版Rust(1.65+),但执行cargo build时编译失败,报错提示anyhow需要使用nightly版本。怀疑问题和backtrace有关,但Rust 1.65已经支持backtrace,不清楚如何解决。


项目相关文件及报错输出

Cargo.toml

[package]
name = "libfs"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = {version = "1.22", features = ["full"]}
anyhow = {version = "1.0.66", features = ["default"]}

src/lib.rs

#[tokio::test]
async fn run_test() {
    println!("test");
}

编译报错输出

$ cargo build
   Compiling anyhow v1.0.66
error[E0554]: `#![feature]` may not be used on the stable release channel
   --> /home/dima/.cargo/registry/src/github.com-1ecc6299db9ec823/anyhow-1.0.66/src/lib.rs:214:32
    |
214 | #![cfg_attr(backtrace, feature(error_generic_member_access, provide_any))]
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
   --> /home/dima/.cargo/registry/src/github.com-1ecc6299db9ec823/anyhow-1.0.66/src/lib.rs:214:61
    |
214 | #![cfg_attr(backtrace, feature(error_generic_member_access, provide_any))]
    |                                                             ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0554`.
error: could not compile `anyhow` due to 2 previous errors

解决方法

问题根源是anyhow 1.0.66的default特性默认包含backtrace,而该版本启用backtrace时依赖了error_generic_member_access和provide_any这两个仅nightly版Rust支持的特性——即便稳定版Rust已经原生支持backtrace,这个版本的anyhow也没适配稳定版API。

有两种可行的解决方式:

  1. 移除anyhow的backtrace特性
    修改Cargo.toml中的anyhow依赖配置,关闭默认特性(默认包含backtrace):
anyhow = {version = "1.0.66", default-features = false}

如果需要backtrace功能但不想切换到nightly,可以直接升级anyhow版本。

  1. 升级anyhow到兼容稳定版的版本
    将anyhow升级到1.0.67及以上版本,这些版本已经修复了该问题,在稳定版Rust下能正常启用backtrace:
anyhow = "1.0.75"  # 或直接指定最新版本号

另外可以用rustc --version确认本地Rust版本确实是1.65及以上的稳定版。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 08:10:28