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

cargo build报错无法选择rand = "^0.9.0"依赖版本如何解决

问题现象

运行cargo build命令时出现依赖解析报错,错误输出如下:

error: failed to select a version for the requirement `rand = "^0.9.0"`
candidate versions found which didn't match: 0.8.5, 0.8.4, 0.8.3, ...
location searched: crates.io index
required by package `guessing_game v0.1.0 (D:\Adwait\Rust\project\guessing_game)`

出问题的项目Cargo.toml配置如下:

[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.9.0"
报错原因

crates.io官方源中目前正式发布的rand包最高稳定版本为0.8.x系列,不存在0.9.0版本,配置中声明的^0.9.0版本要求没有可匹配的候选包,直接导致Cargo依赖解析流程失败。

修复方案

按以下步骤操作即可解决:

  • 打开项目根目录下的Cargo.toml文件,找到[dependencies]配置段
  • 将rand依赖的版本号修改为当前已发布的最新稳定版,修改后对应配置为:
[dependencies]
rand = "0.8.5"
  • 保存文件后重新执行cargo build命令,Cargo会自动拉取对应版本的rand依赖完成编译,报错不再出现。

补充说明:Cargo的语义化版本匹配规则中,^0.9.0代表允许兼容>=0.9.0且<1.0.0的版本,和已发布的0.8.x系列版本大版本不兼容,因此无法自动匹配到可用版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.31 22:54:25