简体   繁体   English

使用 cargo run 时跳过更新 crates.io 索引

[英]Skip Updating crates.io index when using cargo run

I have a simple Program written in Rust.我有一个用 Rust 编写的简单程序。 When I type cargo run in terminal it always shows:当我在终端输入cargo run时,它总是显示:

Updating crates.io index...

And this takes around 40 seconds.这大约需要 40 秒。 But I just wan to execute my Program and I think cargo does not need to update the index every time I run the Program, since this makes testing very slow...但是我只是想执行我的程序,而且我认为 cargo 不需要每次运行程序时都更新索引,因为这会使测试变得非常慢......

Is there an option to skip that?有没有选择跳过它?

I figured it out:我想到了:

Since I am running cargo in a Docker container, I need to store the cargo cache persistently because it resets every time the container restarts.由于我在 Docker 容器中运行货物,因此我需要永久存储货物缓存,因为每次容器重新启动时它都会重置。

There is The Cargo Book that contains all the information you'd ever want to know about cargo . The Cargo Book包含您想知道的关于cargo的所有信息。 See this for disabling index update.请参阅this以禁用索引更新。

I've tried to use this feature myself, and here's the command that worked:我自己尝试使用此功能,这是有效的命令:

cargo +nightly run -Z no-index-update

The +nightly thing is new to me as well, but I find it here . +nightly对我来说也是新事物,但我在这里找到了它。

This answer has been brought up by users thefeiter and Captain Fim but I think a more complete answer could be cool rust/linux newcomers用户thefeiterCaptain Fim提出了这个答案,但我认为更完整的答案可能是酷 rust/linux 新手

When we use docker run, the index is updated every time the container is run because the cache is not shared between runs.当我们使用 docker 运行时,每次容器运行时都会更新索引,因为运行之间不共享缓存。 So to skip the index update, as Captain Fim mentioned, you need to set the CARGO_HOME environment variable on the container.因此,要跳过索引更新,正如 Fim 船长所说,您需要在容器上设置CARGO_HOME环境变量。 This environment variable should contain the path to a persistent folder.此环境变量应包含永久文件夹的路径。 One simple solution is using the docker volumes to share cache between host and container.一种简单的解决方案是使用 docker 卷在主机和容器之间共享缓存。

In my case, I created at cargo_home folder in my project (could be somewhere else) on my host.就我而言,我在主机上的项目(可能在其他地方)的 cargo_home 文件夹中创建。 I have passed the whole project folder to the container and set the docker env variable of CARGO_HOME to the container path to the cargo_home folder.我已将整个项目文件夹传递给容器,并将 CARGO_HOME 的 docker 环境变量设置为 cargo_home 文件夹的容器路径。

The command to build my app looks like this构建我的应用程序的命令如下所示

docker run --rm --user "$(id -u)":"$(id -g)" -e CARGO_HOME=/usr/src/myapp/cargo_home -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust-compiler cargo build

The first time you will run this command, it will take some time, but you should see the cargo_home folder getting filled with files.第一次运行此命令时,需要一些时间,但您应该会看到 cargo_home 文件夹中充满了文件。 The next time you run the command, it should use the cargo_home folder as cache.下次运行该命令时,它应该使用 cargo_home 文件夹作为缓存。 This should be almost instant if your app source code did not change.如果您的应用程序源代码没有更改,这应该几乎是即时的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM