Rust Tutorial
Welcome! Rust is a programming language for writing programs that are fast and that do not crash in strange ways. It is used for web browsers, game engines, command line tools, operating systems, and the servers behind many websites you use every day. Developers have voted it their most loved language for years in a row, and a big reason is the feeling it gives you: when a Rust program compiles, it usually just works.
Rust has a reputation for being hard, so let us be honest about it. Rust asks you to think a little more carefully about your data than languages like Python do. In return, the compiler catches whole families of bugs before your program ever runs. The trick is to see the compiler as a patient teacher rather than a strict examiner. Its messages are long because they are trying to help, and in this tutorial we will learn to read them together.
You do not need to know another language first. If you do, you will spot familiar ideas, and you will also meet a few that are new, such as ownership and borrowing. We take those slowly, with small examples, and only when you are ready for them.
How this tutorial is organised
Each topic explains one idea in plain words, shows short programs, and ends with a few practice exercises. Every example has a Try it in the editor button. It opens the program in our online Rust editor in a new tab, where you can change it, press Run and see what happens. Nothing needs to be installed: the real Rust compiler runs inside your browser.
- First steps: the shape of a program, printing, comments, variables, types, numbers, operators and keyboard input.
- Making decisions and repeating: if, match and the three kinds of loops, then functions of your own.
- Rust's big idea: strings, ownership, borrowing and slices, explained with everyday comparisons.
- Organising data: tuples, arrays, vectors, structs, methods, enums, Option and Result.
- Growing programs: error handling, collections, closures, iterators, traits, generics, lifetimes, modules and files, finished with a small project.
Your first look at Rust
Here is a complete Rust program. Press Try it in the editor, then Run. Cargo, Rust's build tool, compiles it and the terminal shows the result.
fn main() {
println!("Hello, World!");
println!("Rust is ready when you are.");
}Hello, World! Rust is ready when you are.
Do not worry about every symbol yet. fn main() is where the program starts, the curly brackets hold its steps, and println! prints a line. By the end of the next two topics all of it will feel natural.
Practice exercises
- Run the program above, then change the second message to something about yourself and run it again.
- Add a third
println!line that prints your favourite number. - Remove one of the semicolons, press Run, and read what the compiler says. Then put it back.