Search code examples
rustrust-cargosingle-file

How to run Single File Programs in Rust?


I'm trying using Rust in OI (Olympics in Informatics), which only allows submitting Single File Programs.

In C++, we can easily use g++ path/to/file.cpp -oout.exe; ./out.exe. But I haven't found a Cargo command to do that in Rust. The only way I found for now is by adding

[[bin]]
name = "problem_1"
path = "path/to/file.rs"

to Cargo.toml and run cargo run --bin problem_1.

But as I solve more problems, it will soon become super long (and the content is almost repetitive). Is there a better way?


Solution

  • You do not have to use Cargo, the equivalent to your C++ example would be to call rustc directly. It works exactly the same:

    rustc path/to/file.rs -o out.exe; ./out.exe
    

    (It's not clear which operating system or shell you are using, but for example in a Unix-like shell you would usually use && instead of ; to only run the executable if the compilation was successful.)