Learn / Rust / Your first Rust program

Rust, lesson 1 of 5

Your first Rust program

Rust is a programming language built for speed and safety. Browsers, operating systems and security tools use it because it stops whole families of memory bugs before a program ever runs.

Every Rust program starts at a function called main:

fn main() {
    println!("Hello, LearnOfficially");
}

fn starts a function. println! prints a line of text. The ! means it is a macro: a shortcut that writes more code for you. Each instruction ends with a semicolon.

Curly brackets {} inside the text are filled with values, in order:

fn main() {
    println!("{} has {} lessons", "Rust", 5);
}

On your own computer, you make and run a project with Cargo, Rust's build tool:

cargo new hello   # make a new project
cd hello
cargo run         # build it and run it

Check yourself

1. Which function does every Rust program start from?

2. What does the ! in println! tell you?

3. Which command builds and runs a Cargo project?

4. What does println!("{} + {} = {}", 2, 3, 2 + 3) print?