Skip to content
Educora
Beginner14 min1 / 10

What is Rust? Your first program and Cargo

Learn why Rust was created and how it keeps memory safe without a garbage collector, install it with rustup and run your first project with Cargo.

Check yourself
In this lesson you will learn
  • Explain where Rust came from and where it is used
  • Compare three approaches to memory management: manual, garbage collection and ownership
  • Install Rust with rustup and use cargo new, cargo run and cargo build
  • Print text and variables with the println! macro

Imagine a language that runs as fast as C++ but will not let you make memory mistakes: the compiler itself finds the bug before the program ever crashes. Engineers at Microsoft and Google have reported that about 70% of the serious security vulnerabilities in their products come from memory errors. Rust was created to solve exactly this problem, and today it is one of the most loved programming languages.

Rust in a nutshell

Rust is a systems programming language: code is compiled straight to machine code, with no virtual machine or interpreter. It makes three main promises: speed (on the level of C and C++), memory safety (dangling pointers, double frees and data races are caught at compile time) and productivity (the Cargo tool, clear error messages and a strong type system).

  1. 2006
    A personal project

    Mozilla employee Graydon Hoare starts working on Rust as a personal project.

  2. 2009
    Mozilla steps in

    Mozilla begins to sponsor the project officially and develops it for browser technology.

  3. 2015
    Rust 1.0

    The first stable version is released in May. Since then, a new version has come out every 6 weeks.

  4. 2021
    The Rust Foundation

    AWS, Google, Huawei, Microsoft and Mozilla found an independent foundation to support the language.

  5. 2022
    Inside the Linux kernel

    Linux 6.1 adds initial support for writing drivers in Rust.

Where is Rust used?

  • Operating systems — Linux kernel drivers, some Windows components and new parts of Android.
  • Browsers — several parts of Firefox, for example the Stylo engine that computes CSS styles.
  • WebAssembly — Rust code compiles to WebAssembly and runs in the browser at nearly native speed.
  • Command-line tools — fast tools such as ripgrep, fd and bat; the search in the VS Code editor is powered by ripgrep.
  • Servers and the cloud — AWS Firecracker virtual machines and high-load services at Cloudflare and Discord.

Memory safety without a garbage collector

Every program allocates memory while it runs and must give it back later. In C and C++ the programmer does this by hand, and a mistake crashes the program or opens a security hole. In Java, Python and Go a garbage collector cleans up unused memory in the background, but this costs extra memory and time. Rust chose a third way: ownership rules. The compiler tracks who owns each value and frees the memory itself at exactly the right moment.

ApproachLanguagesStrengthWeakness
Manual managementC, C++maximum speed and controlleaks, dangling pointers
Garbage collectorJava, C#, Python, Goconvenient and safepauses, extra memory
Ownership and the borrow checkerRustboth speed and safetyharder to learn at first

Installing with rustup

  1. 1
    Get the installer

    On Windows, download rustup-init.exe from rustup.rs and run it. On macOS and Linux, run the curl command shown below in a terminal.

  2. 2
    C++ build tools on Windows

    On Windows, Rust needs Microsoft's Visual Studio C++ Build Tools. The installer offers to set them up — accept the default options.

  3. 3
    Check the installation

    Open a new terminal and type rustc --version and cargo --version. Both should print a version number.

  4. 4
    Update from time to time

    The rustup update command upgrades Rust to the latest stable version.

Terminal
# macOS / Linux
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# any OS: check the tools
rustc --version
cargo --version
In one go, rustup installs the compiler (rustc), Cargo, the rustfmt formatter, the clippy linter and the standard library documentation.

Your first project with Cargo

Cargo is Rust's official build tool and package manager: it creates projects, compiles code, runs programs, runs tests and downloads libraries. The cargo new command creates a new project:

Terminal
cargo new hello
cd hello
cargo run
Text
hello/
├── Cargo.toml
├── .gitignore
└── src/
    └── main.rs
Cargo.toml holds the project's name, version and dependencies, and the code lives in src/main.rs. Cargo also creates a git repository.
Text
[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[dependencies]
edition is the language edition (2015, 2018, 2021, 2024). New projects get the latest one; the code in this course works in both the 2021 and 2024 editions.
Rust
fn main() {
    println!("Hello, world!");
}
Expected output
Hello, world!
  • fn main() is the entry point; the program starts running here.
  • println! is not a function but a macro — in Rust, macros are recognised by the ! at the end of the name.
  • Text goes in double quotes and the statement ends with ;. Indentation uses 4 spaces.
Text
$ cargo run
   Compiling hello v0.1.0 (/home/aysel/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
     Running `target/debug/hello`
Hello, world!
cargo run first compiles the code (into the target/debug folder) and then starts the program. The timing will differ on every computer.

Printing variables

Inside the println! text, {} is a placeholder: it is replaced by the value given after the comma. You can also put a variable's name straight into the braces: {name}. Variables are created with the let keyword — more on that in the next lesson.

Rust
fn main() {
    let name = "Aysel";
    let year = 2015;
    println!("Hello, {name}!");
    println!("Rust 1.0 was released in {}.", year);
    println!("{} + {} = {}", 2, 3, 2 + 3);
}
Expected output
Hello, Aysel!
Rust 1.0 was released in 2015.
2 + 3 = 5
CommandWhat it does
cargo new namecreates a new project
cargo runcompiles and runs
cargo buildbuilds into target/debug
cargo build --releasebuilds an optimised version into target/release
cargo checkonly checks for errors without producing a file — the fastest
cargo fmtformats the code in the official style
cargo clippyanalyses the code and gives advice
Does not compile
fn main() {
    let name = 'Aysel';
    println("Hello, {name}!");
}
Correct
fn main() {
    let name = "Aysel";
    println!("Hello, {name}!");
}
The left side has two errors. Single quotes are only for one character (char); text goes in double quotes. Calling println without the ! gives error E0423, and the compiler suggests writing println!.

Key points

  • Rust is a compiled systems language that combines speed, memory safety and productivity.
  • Rust has no garbage collector: memory is managed by ownership rules that the compiler checks.
  • rustup installs and updates Rust; Cargo creates, builds and runs projects and downloads libraries.
  • println! is a macro; the placeholders {} or {name} insert values into the text.
  • When speed matters, build an optimised version with cargo build --release.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Which organisation began officially sponsoring Rust's development in 2009?