rust - 使用git添加rust Cargo中的依赖项时是否可以使用路径

标签 rust rust-cargo

我想添加 cargo 依赖项rocket-okapi作为git url,现在我添加了像Cargo.toml这样的依赖项:

rocket-okapi = { git = "https://github.com/GREsau/okapi/tree/master/rocket-okapi"}

但是当我使用 cargo build 命令构建项目时,显示如下错误:

 $ cargo build
    Updating git repository `https://github.com/GREsau/okapi/tree/master/rocket-okapi`
warning: spurious network error (2 tries remaining): unexpected http status code: 404; class=Http (34)
warning: spurious network error (1 tries remaining): unexpected http status code: 404; class=Http (34)
error: failed to get `rocket-okapi` as a dependency of package `fortune v0.1.0 (/workspaces/fortune)`

Caused by:
  failed to load source for dependency `rocket-okapi`

Caused by:
  Unable to update https://github.com/GREsau/okapi/tree/master/rocket-okapi

Caused by:
  failed to fetch into: /home/codespace/.cargo/git/db/rocket-okapi-b6c0b0836896ac76

Caused by:
  network failure seems to have happened
  if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
  https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli

Caused by:
  unexpected http status code: 404; class=Http (34)

如何将子文件夹添加为 Rust cargo 中的依赖项?这可能吗?

最佳答案

这里有一些事情需要澄清。

当您将 Git 依赖项添加到 Cargo.toml 时,它需要一个存储库。您输入的 URL 是存储库中的一个目录,因此 cargo 会崩溃。

其次,当指定 Git 依赖项时,cargo 首先在存储库根目录中查找 Cargo.toml 文件。如果找不到任何文件,它将搜索包名称与依赖项相同的任何 Cargo.toml 文件。

存储库中不存在您指定的依赖项 rocket-okapi,因此 cargo 放弃。将依赖项的名称更改为“rocket_okapi”可以解决此问题。

TL;DR:使用这个

rocket_okapi = { git = "https://github.com/GREsau/okapi" }

关于rust - 使用git添加rust Cargo中的依赖项时是否可以使用路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72528834/

相关文章:

string - 在 Rust 中匹配 Option 静态字符串文字

arrays - 使用 const 泛型将可变指针数组转换为可变引用

parsing - 使用 nom 解析多行注释

docker - Alpine dockerfile : "cannot produce proc-macro...does not support these crate types"

linker - Cargo 创建空的 ELF 文件

rust - Rust 程序如何从其 Cargo 包中访问元数据?

testing - Rust:带有 lib 和二进制目标的 crate 中的属性 #[cfg(test)]

generics - 取决于特征的通用实现

rust - 为什么不能在同一结构中存储值和对该值的引用?

module - 何时/为什么需要导入 Rust 特征 `use` d/Imported