Rust was one of the coolest systems programming languages on the block when I started this series. I wanted the first card to contain the smallest useful loop: write a Rust file, compile it, and run it.
What is the smallest way to compile and run Rust?
One source file is enough. Cargo can wait for the next card.
Reveal the answer
Write main.rs, compile it with rustc main.rs, then run the generated ./main binary.
Set up Rust
Hopefully, you are using Linux. I installed Rust using rustup:
curl https://sh.rustup.rs -sSf | shrustup installs and manages Rust toolchains. Once it finishes, rustc is the compiler I need for this first small program.
Write one tiny program
I created a directory and a file named main.rs:
mkdir -p ~/rust_sources/ahoy
cd ~/rust_sources/ahoy
touch main.rsThe whole program is two lines:
fn main() {
println!("ahoy ahoy!");
}Compile and run it
rustc main.rs
./mainahoy ahoy!
rustc reads main.rs and writes an executable named main in the same directory. Running that executable prints the message.
What I keep in my head
For one small Rust file, the loop is simply rustc file.rs followed by running the generated binary.