Flashcard Rust: First steps

A first Rust flashcard: install Rust, compile a tiny program with rustc, and run the resulting binary from the command line.
coding
rust
Author

unrahul

Published

March 7, 2020

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.

Flashcard 01 Rust

The question

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 | sh

rustup 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.rs

The whole program is two lines:

fn main() {
    println!("ahoy ahoy!");
}

Compile and run it

rustc main.rs
./main
ahoy 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.