Introduction to Rust

Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — it compiles to native machine code yet guarantees memory safety at compile time, with no garbage collector.

Learn Introduction to Rust in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll understand what Rust is and why it exists, how Cargo builds and runs your code, and you'll have written and run your own first Rust program that prints to the terminal.

What You'll Learn in This Lesson

1️⃣ What is Rust?

Rust is a systems programming language — the kind used to build software where performance and reliability matter most, like operating systems, browsers, and game engines. It first appeared in 2010 and was designed to solve a long-standing tension: languages like C and C++ are blazingly fast but make it easy to introduce memory bugs, while safer languages usually pay for that safety with a garbage collector and slower runtime. Rust gives you both. Its core ideas are:

You'll start exactly where every Rust developer starts: a tiny program that prints text. Read the worked example below first — every line is explained — then run it yourself.

2️⃣ Why Rust? Safety Without a Garbage Collector

What makes Rust special is when it catches mistakes: at compile time , before the program ever runs. In many languages a bug like using freed memory shows up later as a crash in production. Rust refuses to compile such code. It does this with a system of ownership (which you'll study in depth later) that lets it free memory automatically — no garbage collector required. Read the comments below, then run it.

Notice that language and year were declared with plain let and never changed. In Rust, variables are immutable by default — a small rule that prevents a whole class of bugs. You'll learn how to opt into mutability with mut in the next lesson.

Your turn. The program below works once you fill in the three blanks marked ___ . Follow the 👉 hints, then run it and compare with the expected output.

3️⃣ Building and Running with Cargo

Cargo is Rust's build tool and package manager, installed automatically with Rust. Instead of calling the compiler by hand, you let Cargo create a project, fetch dependencies, build your code, and run it. The commands below are the everyday workflow of a Rust developer.

No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it with cargo run , and check your output against the example in the comments. This is exactly the kind of tiny program that builds real fluency.

📋 Quick Reference — Rust Basics

Practice quiz

Where does every runnable Rust program start executing?

  • The start function
  • The first line of the file
  • The fn main() function
  • The run() method

Answer: The fn main() function. Execution begins in fn main(), the program's entry point.

Why is println! written with an exclamation mark?

  • It is a macro
  • It is a regular function
  • It throws an error
  • It marks a constant

Answer: It is a macro. println! is a macro, and macros are invoked with a trailing !.

What does the {} do inside a println! string?

  • Comments out text
  • Starts a code block
  • Escapes a quote
  • Acts as a placeholder for a value

Answer: Acts as a placeholder for a value. {} is a placeholder filled by the value given after the comma.

How does Rust manage memory?

  • With a garbage collector
  • Through ownership rules at compile time
  • Manually with free()
  • It never frees memory

Answer: Through ownership rules at compile time. Rust uses ownership to free memory automatically with no garbage collector.

By default, Rust variables declared with let are:

  • Immutable
  • Mutable
  • Global
  • Constant expressions

Answer: Immutable. Variables are immutable by default; you opt into change with mut.

When does Rust catch many memory bugs?

  • At runtime only
  • Never
  • At compile time
  • During garbage collection

Answer: At compile time. Rust refuses to compile unsafe code, catching bugs before the program runs.

Which command creates a new Cargo project called hello?

  • cargo build hello
  • cargo new hello
  • cargo run hello
  • cargo init --name

Answer: cargo new hello. cargo new hello creates a folder with starter project files.

What does cargo run do?

  • Only downloads dependencies
  • Formats the code
  • Deletes the target folder
  • Builds and runs the program

Answer: Builds and runs the program. cargo run compiles src/main.rs and then runs the resulting program.

What kind of code does Rust compile to?

  • Bytecode for a virtual machine
  • Native machine code
  • JavaScript
  • An interpreted script

Answer: Native machine code. Rust compiles straight to native machine code, so it runs as fast as C.

Which tool is installed alongside Rust to build and manage projects?

  • npm
  • Make
  • Cargo
  • pip

Answer: Cargo. Cargo is Rust's build tool and package manager, installed with Rust.