Cargo Error: failed to get `x` as a dependency of package解决方法

在使用Cargo运行或编译有依赖的程序时,可能会出现类似下面这种错误:

PS D:\hocn\Desktop\rust\a1> cargo run    
    Updating crates.io index
error: failed to get `image` as a dependency of package `a1 v0.1.0 (D:\hocn\Desktop\rust\a1)`

Caused by:
  failed to load source for dependency `image`

Caused by:
  Unable to update registry `crates-io`

Caused by:
  failed to fetch `https://github.com/rust-lang/crates.io-index`

Caused by:
  failed to authenticate when downloading repository: git@github.com:rust-lang/crates.io-index

  * attempted ssh-agent authentication, but no usernames succeeded: `git`

  if the git CLI succeeds then `net.git-fetch-with-cli` may help here
  https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli

Caused by:
  error authenticating: unable to connect to agent pipe; class=Ssh (23)

原因

默认情况下,Cargo是通过内置的git库来下载依赖的,有时会因为验证原因导致失败;

解决办法

  • 找到.cargo目录,以Windows系统为例:C:\Users\hocn\.cargo,注意用户名;
  • 进入.cargo目录,编辑文件config.toml,如没有则创建;
  • 写入以下内容:
  • [net]
    git-fetch-with-cli = true

git-fetch-with-cli默认为false,表示使用内置的git库来下载依赖;为true时,使用git程序来下载依赖,这样就不存在验证失败的问题;

请确保你安装了git程序并正确配置;

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/p/failed-to-get-x-as-a-dependency-of-package.html

分类: 计算机技术
推荐阅读:
Golang实现线性搜索算法(Linear Search) 本文将使用Go语言实现线性搜索算法(Linear Search);
Python计算卡特兰数(catanlan number) 卡特兰数(Catalan number),是组合数学中一种常出现于各种计数问题中的数列;本文使用Python来计算卡特兰数;
如何将Rust更新到最新版 Rust每隔一段时间就会有新的版本,若要将已安装的Rust更新到最新版,并不用先卸载旧的版本,只需要使用 rustup 工具;
PySide6 QWidget更改窗口标题 PySide6.QtWidgets.QWidget类的成员函数setWindowTitle()用于为窗口更改标题;
Linux查看经常使用的命令 在Linux系统下,如果你想查看你常常使用的命令,那你运行一条命令即可实现:
Python hasattr()函数 hasattr() 是 Python 内置函数之一,用于检查一个对象是否具有指定的属性或方法。