有没有办法重命名 Cargo 项目?

Is there any way to rename a Cargo project?

我使用以下方法创建了一个项目:cargo new projectname --bin。 如何将 projectname 更改为其他名称?

我检查了man page and the Cargo documentation。我也运行:

在元数据文件中(Cargo.toml、Cargo.lock、...),有 "name" 和 "path"。我想我可以手动更改它们,但我不知道这是否会破坏任何东西。

最好的方法是什么?

我认为你应该手动更改它。没那么难,真的。

我运行这个代码:

$ cargo new smurf --bin
     Created binary (application) `smurf` project
$ cd smurf/
smurf$ cargo build
     ....
smurf$ grep -rl smurf .
./target/debug/smurf.d
./target/debug/smurf
./target/debug/.fingerprint/smurf-35f069edf7faaa12/bin-smurf-35f069edf7faaa12.json
./target/debug/.fingerprint/smurf-35f069edf7faaa12/dep-bin-smurf-35f069edf7faaa12
./target/debug/deps/smurf-35f069edf7faaa12
./Cargo.lock
./Cargo.toml

从所有这些文件中,整个 target 可能只是被删除了。 .lock 文件也可以删除。 Cargo.toml...好吧,你可以编辑它。

我尝试只更改 Cargo.toml,一切正常。但是,您最终会在 target 中得到无用的文件,因此我建议您还是删除这些文件。

在 Linux 下,这是相当困难的:

  • 转到您的项目所在的目录,例如如果你的项目在名为 rust 的文件夹中被称为 hello_world 然后转到 rust 文件夹 pi@raspberrypi:~/workspace/rust/hello_world $ cd ..
  • 您可以从那里重命名项目
    1. mv [项目的当前名称] [您要移动的名称]。例如。如果我想将它从 hello_world 重命名为 hello_rust 我会输入 mv hello_world/ hello_rust/ 重命名文件夹。
    2. 现在您只需更改 Cargo.toml 文件中的名称:
      • pi@raspberrypi:~/workspace/rust $ cd hello_rust/
      • pi@raspberrypi:~/workspace/rust/hello_rust $ geany Cargo.toml
      • (我使用的是 geany,但您可以使用任何您喜欢的文本编辑器)
      • 在Cargo.toml第二行改成
      • name = "hello_world"name = "hello_rust"

希望这对以后的人有所帮助