Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active July 6, 2021 18:50
Show Gist options
  • Save rpivo/55759d47bc826295ce200f93c6398f6e to your computer and use it in GitHub Desktop.
Save rpivo/55759d47bc826295ce200f93c6398f6e to your computer and use it in GitHub Desktop.
Running a Rust File From the Command Line

Running a Rust File From the Command Line

To run a Rust file, say main.rs, you first need to compile it with rustc, which will produce an executable. Then you can run the executable.

rustc main.rs
./main

If a project is Cargo-ized, then you can use Cargo to compile it.

cargo build && target/debug/hello_world

This will first build the source code, and then it will run the executable.

You can also perform the above commands in one fell swoop with cargo run:

cargo run

If you want to check that your code will compile without actually producing an executable, use cargo check.

cargo check

If you want to build the source code with optimizations, use cargo build --release. This will produce output in target/release, and should be optimized. It may take longer to build with the --release flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment