Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 132 ยท Getting Started

Getting Started with Rust

Learn what Rust is, why it is fast and memory safe without a garbage collector, who uses it, what editions are, and run your first Rust program right in the browser.

Rust is a systems programming language. That phrase means it is meant for software that sits close to the machine and has to be fast and reliable: web browsers, operating system kernels, databases, game engines, command-line tools, network services, and firmware for tiny devices. For decades such software was written almost entirely in C and C++, which give you total control but also let you make mistakes that crash programs or open security holes. Rust gives you the same level of control and speed, and then adds something remarkable: the compiler refuses to build programs that contain whole families of those mistakes.

This lesson explains what Rust is, what makes it different, where it is used, and how the language is released and versioned. Then you will write and run your first programs right here in the page. The next lesson looks under the hood at what happens between your source file and a running program.

What makes Rust different

Three ideas appear in nearly every description of Rust, and each one deserves a plain explanation.

Memory safety without a garbage collector. Languages like Java, Go, Python and C# keep programs safe by running a garbage collector: a background system that finds memory you no longer use and frees it. That is convenient, but it costs time and memory, and it makes the speed of your program less predictable. C and C++ have no such system, so you free memory yourself, and when you free it too early, too late, or twice, you get bugs such as use-after-free and double-free. Rust takes a third path. Every value has exactly one owner, and when the owner goes out of scope the value is freed, at a point the compiler knows at build time. You will meet this rule in the ownership lesson. There is no collector running at run time, and no way to forget to free memory.

Zero-cost abstractions. You can write high-level code with iterators, closures, generics and traits, and the compiler turns it into machine code about as fast as the loop you would have written by hand. Abstractions cost you nothing at run time; you pay only for what you actually use.

Fearless concurrency. The same ownership rules that prevent memory bugs also prevent data races, the bugs that happen when two threads touch the same data at once and at least one of them writes. In Rust a data race is a compile error, not a rare crash that shows up in production once a month. The concurrency chapter builds on this.

Together this is the promise of the language: if it compiles, a large class of bugs cannot be there. The price is that the compiler is strict, and in the beginning it will reject code that you believe is fine. Reading its messages is a skill, and this tutorial teaches it from the very start.

Rust does not guarantee that your program is correct. It guarantees memory safety and freedom from data races in safe code. A program can still compute the wrong answer or crash on purpose with a panic. The language moves a large part of the bug-hunting from run time to compile time, which is a very good trade, but it is not magic.

Who uses Rust

Rust started as a personal project of Graydon Hoare, was sponsored by Mozilla for the Firefox browser, and had its first stable release (1.0) in May 2015. Since then it has spread widely:

  • Browsers and runtimes. Parts of Firefox are written in Rust, and several JavaScript tooling projects (bundlers, linters, formatters, and a popular runtime) are too.
  • Operating systems. Rust is an accepted second language for the Linux kernel, and it is used in parts of Windows, Android and other systems.
  • Cloud and networking. Proxies, databases, message queues and serverless platforms use Rust where latency and memory use matter.
  • Command-line tools. Fast search tools, file managers and developer utilities are often written in Rust, because a single compiled binary is easy to install.
  • Embedded and WebAssembly. Rust runs on microcontrollers with a few kilobytes of memory, and compiles to WebAssembly for the browser.

You will not need any of those to learn the language. Everything in this tutorial runs as a single file in your browser.

Your first program

Here is the classic first program. Press Run to compile it on our server and run it:

Rust Hello, Rust

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Let us read it piece by piece.

  • fn starts a function definition. main is the function name. Every executable Rust program starts running in a function called main.
  • () is the (empty) list of parameters. { ... } is the function body.
  • println! prints a line of text. The exclamation mark tells you this is a macro, not an ordinary function. Macros are code that writes code at compile time, and println! needs that power because it checks your format string while the program is being compiled. Printing covers it in detail.
  • The text in double quotes is a string literal.
  • The ; at the end finishes the statement. The next lesson on syntax explains exactly where semicolons belong and why.

Notice what is not there: no import line, no class, no public static void. A Rust program can be just a function. The standard library's most common items (including println!) are available automatically.

Doing something more

A program that prints a fixed line is not very exciting. This one stores values in variables, does arithmetic and prints the results with placeholders:

Rust Variables and placeholders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

let creates a variable. You did not have to write a type: the compiler worked out that language is a string slice and the two numbers are 32-bit integers. This is called type inference, and it is one reason Rust code feels lighter than its strictness suggests. Inside the quotes, is replaced with the value of the variable of that name. The subtraction is ordinary integer arithmetic.

Variables in Rust are immutable by default: once you give age a value you cannot change it. That surprises many beginners, and it is deliberate. When you do need a changing value, you ask for it with mut:

Rust A mutable variable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Try deleting the word mut and running again to see the compiler refuse the change. The variables lesson explains why this default helps you.

What the compiler tells you

Rust has a reputation for friendly compiler messages. Every mistake gets an error code, a pointer to the exact spot in your code, and usually a suggestion. Here is a program with a typo in a variable name. Press Run and read the message:

Rust A first compile error (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The compiler reports error[E0425]: cannot find value mesage in this scope, points with an arrow at line 3, and even adds a help line suggesting the name you probably meant, message. Nothing was compiled and nothing ran, because in Rust an error stops the build. Fixing it is a matter of spelling the name correctly.

Get into the habit of reading these messages from the top. The first line says what is wrong, the arrow says where, and the help or note lines often say how to fix it. When a program produces many errors, fix the first one and recompile: later errors are often just consequences of the first.

The error code (here E0425) is a searchable identifier. You can look up any code with rustc --explain E0425 on your own computer, which prints a long explanation with examples. The tutorial's compiler errors lesson collects the most common ones.

How we run code in this tutorial

Every rust example on this site has an editor and a Run button. When you press it, your code is sent to a server that compiles it with the real Rust compiler and runs the program in an isolated sandbox. What you see in the output area is what the program printed. A few things to know:

  • The code is one file with one main function, and it can use only the standard library. Real projects use Cargo and external crates, which the Cargo lesson shows in read-only blocks.
  • The build uses the 2024 edition with debug assertions and overflow checks on, so it behaves like a debug build: arithmetic that overflows panics instead of silently wrapping.
  • Programs have a few seconds to finish and a limit on how much they can print, so keep experiments small.
  • You can edit any example. Changing code and re-running is the fastest way to learn, and breaking it on purpose to read the error is even better.

Editions and the release rhythm

Rust is released on a fixed schedule: a new stable version every six weeks. This tutorial uses version 1.99. The six-week rhythm means that features arrive in small, predictable steps instead of big risky releases, and a new compiler almost always compiles old code without changes. Rust promises backward compatibility for stable code.

Sometimes the language wants to make a change that would break old code, such as reserving a new keyword. Rust handles this with editions: an opt-in setting per crate. There have been four so far: 2015, 2018, 2021 and 2024. Each crate declares which edition it uses, and crates of different editions can be linked together without trouble, because editions change the surface syntax, not the compiled result. This tutorial uses the 2024 edition, which brought features such as if let chains and changes to how temporaries and impl Trait behave. Whenever a lesson uses something specific to a recent version, it names the version.

Because Rust is young, you will sometimes see old advice online that no longer applies, for example the try! macro, extern crate lines, or hand-written Box<dyn Error> boilerplate that later syntax replaced. This tutorial teaches the modern way and mentions old habits only as warnings.

What you will learn

The tutorial goes in this order. First the basics: variables, types, control flow. Then the heart of Rust: ownership, borrowing and lifetimes. After that come structs, enums and traits, generics, collections and iterators, error handling, modules and macros, smart pointers, concurrency, async, the standard library, testing, and advanced topics. Seven complete projects close the course.

Be patient with the ownership chapters. Almost everyone finds them hard at first, because they ask you to think about who owns data and for how long, something other languages hide from you. The reward is that once the model clicks, you will understand your programs' memory more clearly than you ever did in other languages.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How does Rust free memory that is no longer used?

    Each value has one owner, and when the owner goes out of scope the value is dropped. The compiler decides this at build time, so there is no collector and no manual free.
  2. 2. What does the exclamation mark in println! tell you?

    A trailing ! marks a macro invocation. println! is a macro so that it can check the format string at compile time.
  3. 3. How often does a new stable Rust version appear, and what is an edition?

    Stable releases come on a six-week schedule. Editions let the language make small syntax changes without breaking older code, and each crate chooses its own.
  4. 4. You write let x = 5; and later x = 6;. What happens?

    Variables are immutable by default. Assigning twice to a non-mut variable is a compile error; you would write let mut x = 5;.
  5. 5. What does Rust guarantee for safe code?

    Safe Rust rules out use-after-free, double-free, dangling references and data races. It does not rule out logic errors or deliberate panics.

Summary

  • Rust is a fast, compiled systems language that achieves memory safety through ownership instead of a garbage collector.
  • Zero-cost abstractions mean high-level code compiles to efficient machine code.
  • The same rules that protect memory also prevent data races, which is what "fearless concurrency" means.
  • Every program starts in fn main, and println! (a macro, because of the !) prints a line.
  • let creates variables, which are immutable by default; types are usually inferred.
  • Compile errors have codes such as E0425, point at the problem, and often suggest a fix. Read the first error first.
  • A new stable release comes every six weeks, and editions (2015, 2018, 2021, 2024) allow syntax changes without breaking old code. This tutorial uses Rust 1.99 and the 2024 edition.

Next: How Rust Works: rustc, Cargo and the Toolchain