Skip to content
DocsRust LearningbeginnerGetting Started
Chapter 1 of 19·beginner·5 min read

Getting Started

Bắt Đầu

Install Rust and write your first program

Hover or tap any paragraph to see Vietnamese translation

What is Rust?

Rust is a systems programming language focused on three goals: performance, reliability, and productivity. With no garbage collector, Rust guarantees memory safety through a powerful type system and a unique ownership model.

Rust is suitable for many domains: systems programming, web development via WebAssembly, command-line tools, network services, and embedded systems. Created by Mozilla, it is now used by Microsoft, Google, Amazon, and many other major companies.

Installing Rust with rustup

The recommended way to install Rust is via rustup — the official Rust toolchain installer. rustup lets you install, update, and manage multiple Rust versions simultaneously, and includes rustc (the compiler), cargo (the package manager), and other essential tools.

Installing on macOS and Linux

Open a terminal and run the following command. This script downloads and installs rustup along with the latest stable Rust toolchain. After it finishes, reload your shell configuration.

Terminal
1curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh23# After installation, reload your shell:4source "$HOME/.cargo/env"

Installing on Windows

On Windows, download the rustup-init.exe installer from https://rustup.rs and run it. You also need to install the Visual Studio C++ Build Tools for the Rust compiler to work.

Warning
On Windows, Rust requires C++ build tools from Visual Studio. If you do not have them, the installer will guide you. Select the "Desktop development with C++" workload in the Visual Studio Installer.

Verifying Your Installation

After installation completes, verify everything is working by checking the versions of the core tools. Open a new terminal and run the following commands.

Terminal
1rustc --version2cargo --version3rustup --version45# Example output:6# rustc 1.78.0 (9b00956e5 2024-04-29)7# cargo 1.78.0 (54d8815d0 2024-03-26)8# rustup 1.27.0 (bbb9276d2 2024-03-08)910# Update to the latest stable Rust at any time:11rustup update

Creating Your First Project with cargo new

Cargo is Rust's build system and package manager. It handles compiling your code, downloading and compiling library dependencies, and building distributable packages. To create a new project, use cargo new.

Terminal
1# Create a new binary (executable) project2cargo new hello_world34# Create a new library project5cargo new my_library --lib67# Navigate into the project8cd hello_world

The cargo new command creates a new directory with the following structure. Cargo.toml is the project configuration file and src/main.rs is the program entry point.

Project Structure
1hello_world/2├── Cargo.toml3└── src/4    └── main.rs

Hello, World!

When you create a new project with cargo new, the src/main.rs file already contains a Hello, World! program. This is the most basic Rust syntax and a good place to start understanding the language.

src/main.rs
fn main() {
    println!("Hello, world!");
}
  • fn main() declares the main function — the entry point of every Rust executable. It takes no parameters and returns nothing.
  • println! is a macro (the ! indicates this, not a regular function) that prints text to stdout with a trailing newline.
  • Rust uses four spaces for indentation by convention, not tabs. rustfmt (the code formatter) enforces this automatically.

Essential Cargo Commands

Cargo provides a set of commands to manage the Rust project lifecycle. These are the commands you will use every day during development.

Terminal
1# Compile the project (debug build in target/debug/)2cargo build34# Compile and immediately run the program5cargo run67# Check for errors without producing a binary (much faster)8cargo check910# Build with full optimizations (release build in target/release/)11cargo build --release1213# Run tests14cargo test1516# Generate and open HTML documentation17cargo doc --open1819# Add a dependency to Cargo.toml20cargo add serde
  • cargo build: Compiles the project and creates a debug binary. The first build takes longer because dependencies are also compiled. Subsequent builds are faster due to caching.
  • cargo run: Combines build and execution in one step. Very convenient during development.
  • cargo check: Only checks for errors without producing a binary — much faster than cargo build. Use it frequently to catch errors early while writing code.
  • cargo build --release: Produces a fully optimized production build. The binary runs significantly faster but compilation takes longer.

IDE Setup

VS Code with the rust-analyzer extension is the most popular development environment for Rust. rust-analyzer provides code completion, type information, code navigation, refactoring, and real-time error diagnostics.

  • Install VS Code from https://code.visualstudio.com if you do not have it already.
  • Open Extensions (Ctrl+Shift+X), search for "rust-analyzer" and install it. This is the official extension maintained by the Rust team.
  • Optional: Install the "Even Better TOML" extension for better Cargo.toml syntax support with autocompletion and validation.
  • Optional: Install the "CodeLLDB" extension for debugging support with breakpoints and variable inspection.
Tip
rust-analyzer automatically downloads necessary components when you first open a Rust project. This process may take a few minutes depending on your internet connection speed.

Cargo.toml Basics

Cargo.toml is the Rust project configuration file, written in TOML format. It contains project metadata and the list of libraries (crates) the project depends on.

Cargo.toml
1[package]2name = "hello_world"3version = "0.1.0"4edition = "2021"56[dependencies]7# Add external crates from crates.io here8serde = { version = "1", features = ["derive"] }9tokio = { version = "1", features = ["full"] }1011[dev-dependencies]12# Dependencies only used in tests — not included in production builds13criterion = "0.5"1415[profile.release]16opt-level = 3    # Maximum optimization17lto = true       # Link-time optimization
  • [package]: Contains project metadata. edition = "2021" is the recommended Rust edition, providing the most modern syntax and features.
  • [dependencies]: Lists the external crates the project needs. Cargo automatically downloads and compiles them from crates.io when you run cargo build.
  • [dev-dependencies]: Dependencies only used during development and testing, not included in production builds.
Info
crates.io is the central registry for the Rust ecosystem with tens of thousands of open-source libraries. Documentation for all published crates is automatically generated and published at docs.rs.

Key Takeaways

Điểm Chính

  • Rust is installed via rustup, which manages the toolchainRust được cài đặt thông qua rustup, công cụ quản lý toolchain
  • Cargo is Rust's build system and package managerCargo là hệ thống build và trình quản lý gói của Rust
  • cargo run compiles and runs your program in one stepcargo run biên dịch và chạy chương trình trong một bước
  • Cargo.toml defines project metadata and dependenciesCargo.toml định nghĩa metadata dự án và các dependency

Practice

Test your understanding of this chapter

Quiz

What is Rust's official build system and package manager?

Hệ thống build và trình quản lý gói chính thức của Rust là gì?

True or False

Rust source files use the .rs file extension.

File mã nguồn Rust sử dụng phần mở rộng .rs.

Code Challenge

Complete the cargo command to compile and run a Rust project

Hoàn thành lệnh cargo để biên dịch và chạy dự án Rust

cargo 
Quiz

Which file contains the project metadata and dependencies in a Rust project?

File nào chứa metadata dự án và các dependency trong dự án Rust?

True or False

The cargo check command produces a compiled binary in the target/ folder.

Lệnh cargo check tạo ra một binary biên dịch trong thư mục target/.

Chapter Complete!

Great job! Keep the momentum going.

Your progress0 of 19 chapters read
← → to navigate chapters
Built: 4/8/2026, 12:01:11 PM