13

How can I make a program in Rust which can be executed from anywhere without using cargo run, by just clicking on the file?

Is there any crate? I have written code for snake game and I want to run it by just clicking on a file.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
shakaib naqvi
  • 153
  • 1
  • 1
  • 6
  • 1
    The executable is created in the `target/release` directory when you run `cargo build --release`. Note that it might not be executable from _anywhere_, but only from machines that use the operating system and hardware compatible with the one you are using; e.g. if you build on PC running Windows, the resulting executable will run on other PCs running Windows. Is that what you are looking for? – user4815162342 Mar 31 '20 at 06:20
  • 1
    Also, to expand on the above comment, this executable is automatically self-contained and can be run by clicking on it. No crate needed, and no cargo. It should work everywhere that uses the platform for which you compiled (e.g. Windows) unless you use specific crates with system library dependencies. – nnnmmm Mar 31 '20 at 07:44

4 Answers4

21

If you compile a Rust application with:

cargo build --release

it will place a binary file in ./target/release. So if your application is called snake_game, you can run it with ./target/release/snake_game, or by double-clicking on that file.

This binary is completely self-contained, so you can move or copy it to somewhere else on your computer.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • As OP mentions a game, I assume there are resources ([How to embed resources in Rust executable?](https://stackoverflow.com/q/27140634/155423)). As OP mentions "click", I assume they are on Windows. – Shepmaster Mar 31 '20 at 14:44
  • By this way binary file has been placed but it is not opening! Is it for linux also? I am a linux user. – shakaib naqvi Mar 31 '20 at 15:04
12

First build your binary for release

cargo build --release 

Next handle permissions

Typically chmod +x target/release/whateverYourProgramIsCalled to make executable, but cargo did this for us already

You can check it's Octal Permissions

ls -l target/release/whateverYourProgramIsCalled
chmod +x target/release/whateverYourProgramIsCalled
ls -l target/release/whateverYourProgramIsCalled

As you can see nothing has changed... the permissions were already correct for executing

Run that executable

./whateverYourProgramIsCalled 

Optional: run from anywhere

You can run that binary anywhere from the command line

To do this you need to add it to your path

For mac you can add to your path from /etc/paths

whatever editor you prefer.... vi, code, etc...

sudo code /etc/paths

I added a path like this, saved and authenticated with a password

/Users/`whoami`/code/rust/binaries

whoami command is surrounded by `

next copy your new binary over to where it needs to be, in that binaries folder

cp whateverYourProgramIsCalled /Users/`whoami`/code/rust/binaries

Then open a new terminal window

Check your command is in your path

where whateverYourProgramIsCalled

Run your command from anywhere

whateverYourProgramIsCalled 

Rejoice

jasonleonhard
  • 12,047
  • 89
  • 66
1

There is another method you can do by using rustc. It will create an executable binary file in the same directory where your file exists.

Make sure you are in the src directory and the name of your file is main.rs.

rustc main.rs
./main

The advantage of using rustc is that you can run any file not only main.rs. just do:

 rustc filename.rs
 ./filename

You can run it from terminal and also by clicking on that file.

Jawwad Turabi
  • 322
  • 4
  • 12
  • The disadvantage is that you need to download every single dependency manually, and then pass each as an argument to `rustc`. I can't see any advantages to this. – Peter Hall Feb 10 '21 at 15:01
  • 2
    Also, with `cargo`, you can specify other binary entry points besides `main.rs` in `[bin]` sections of your `Cargo.toml`. – Peter Hall Feb 10 '21 at 15:02
1

Just wanted to add another answer here, not sure if it's related to a more recent version of rust.

If in the root of your project you just run the command cargo install --path . it will add it to cargo and allow you to run the binary just with project name.

Sam
  • 773
  • 4
  • 13