Is there a way to compile with a different filename?

In Rust, is there a way for me to generate an executable but with a different name, can I somehow specify this via cargo or something?

Yes you can, it's outlined in the Cargo Reference in Chapter 3 Section 2.1, in the subsection on binaries. I would suggest reading the reference.

https://doc.rust-lang.org/cargo/reference/cargo-targets.html

2 Likes

https://doc.rust-lang.org/cargo/reference/cargo-targets.html#binaries

I am looking over here but I want the filename to have a complete different file extension name as well.

[[bin]]
name = "cool-tool"

I know I can do this but like how would I specify what the executable's extension name would be?

You can't currently, this is platform-dependent and cargo automatically decides which file extension to use based on the platform and target configuration. If I may ask, what is your reason for wanting to change the extension of your output binary?

So I want to write some Rust code which can delete certain files on my computer. THe problem is that if I wrote it incorrectly I could end up deleting stuff that it shouldn't delete so I want to test it on a VM rather. Yes I can drag and drop but the problem is that what if I accidently double clicked on it?

I suppose the "easy" fix for this is to use a batch script that will compile the code and then rename the program which would work.

Yes, this would be the current recommended approach. I've seen a number of projects that rely on a shell script for the entire build process, which you could do as well.

2 Likes

Are you referring to Rust projects that uses the cargo package manager?

Either with cargo, or using rustc directly, it really depends on the use case of the project.

In your case, you could simply create a build.cmd file for Windows containing something like the following to rename cool-tool.exe to cool-tool.ext:

cargo build --release
rename "%~dp0\target\release\cool-tool.exe" cool-tool.ext

What is the difference betweem *.cmd and *.bat? I always never knew the difference?

You could make the deletion behavior dependent on a command line argument or an environment variable. This would avoid a double click causing it to delete the files.

I could but I rather just click on it and it executes.

Well, you could make the behavior dependent on the file name. Have it do nothing with the default file name but if you change it to something it enables the deletion behavior.

I could actually do that instead. Thanks mate :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.