Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

Rust Get Started

Before we write more code, it helps to know what happens when you press Run. Rust is a compiled language. That means your code is not run directly. First a program called the compiler (for Rust it is called rustc) reads all of your code, checks it very carefully, and translates it into a program the computer can run. Only then does your program start.

You can think of the compiler as a careful editor who reads your whole essay before it is published. If something does not make sense, the essay goes back to you with notes in the margin. That is why Rust catches so many mistakes early: they are found while compiling, not while your program is already running for a user.

Cargo, the helper

Most Rust programmers never call the compiler by hand. They use Cargo, Rust's build tool. Cargo calls the compiler for you, keeps track of the files in your project, and runs the finished program. The Run button in our editor simply types cargo run in the terminal, so you see exactly what a Rust programmer sees on their own computer:

cargo run

Cargo first prints a line that starts with Compiling, then Finished, then Running, and after that comes the output of your own program. The first build of a visit takes a little longer because the compiler itself has to start in your browser. After that, small programs build in a few seconds.

Your first program

Here is a program that prints two lines. Open it in the editor and run it:

fn main() {
    println!("My first Rust program!");
    println!("2 + 3 = {}", 2 + 3);
}
Output
My first Rust program!
2 + 3 = 5

The second line shows a nice trick: the {} inside the text is a placeholder, and Rust fills it with the value after the comma. Here that value is 2 + 3, which Rust works out to be 5.

When something goes wrong

Mistakes are completely normal, and you will make lots of them. Everyone does. When the compiler finds a problem, it stops and prints an error that says what is wrong and on which line. In our editor the line is also marked in red, and a short hint in plain words appears below the message. Read the first line of the error first; it usually tells you most of the story.

Practice exercises

  1. Run the program above, then change 2 + 3 to 20 * 4 and fix the text so that it still matches.
  2. Type cargo run yourself in the terminal and press Enter. Compare it with pressing the Run button.
  3. Type rustc --version in the terminal to see which version of the compiler you are using.