20

I created a project using: cargo new projectname --bin. How do I change projectname to some other name?

I checked the man page and the Cargo documentation. I also ran:

  • cargo help
  • cargo --list
  • man cargo

In the metadata files (Cargo.toml, Cargo.lock, ...), there is "name" and "path". I suppose I could change them manually, but I don't know if that would break anything.

What would be the best way to do this?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
David Sainez
  • 6,446
  • 1
  • 22
  • 45

2 Answers2

19

I think that you should change it manually. Not so difficult, really.

I run this code:

$ 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

From all these files, the whole target may be just deleted. The .lock file can also be deleted. And the Cargo.toml... well you can just edit it.

I tried changing only the Cargo.toml and all just works. However you end up with useless files in target so I recommend deleting those anyway.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
rodrigo
  • 94,151
  • 12
  • 143
  • 190
5

Under Linux this is fairly strait forward:

  • go to the directory where your project is located e.g. if your project was called hello_world in a folder named rust then go to the rust folder pi@raspberrypi:~/workspace/rust/hello_world $ cd ..
  • from there you can rename the project by
    1. mv [current name of the project] [name you want] to move. E.g. if I wanted to rename it from hello_world to hello_rust I would type mv hello_world/ hello_rust/ to rename the folder.
    2. now you only have to change the name in your Cargo.toml file:
      • pi@raspberrypi:~/workspace/rust $ cd hello_rust/
      • pi@raspberrypi:~/workspace/rust/hello_rust $ geany Cargo.toml
      • (I am using geany but you can use whatever text editor you like)
      • in Cargo.toml in the second line change
      • name = "hello_world" to name = "hello_rust"

Hope that this might help someone in the future

Julius_Gu
  • 61
  • 1
  • 1
  • TL;DR - Just make a copy of the original package, open the copied directory in your IDE, and change `name` in the Cargo.toml file, but just make sure the new name matches the new directory's name. – Benji Dec 10 '22 at 11:02