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.