Chapter 2: Getting Started

This chapter guides you through setting up your Rust development environment and writing your first program. By the end, you'll have a fully configured workspace and understand the basics of Cargo, Rust's build system and package manager.

2.1 Installing Rust

Rust is installed through rustup, the official Rust toolchain installer. It manages Rust versions and associated tools, making it easy to keep your installation up to date.

macOS and Linux

Open your terminal and run:

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

This command downloads and runs the rustup installer. Follow the on-screen prompts (usually pressing Enter for default options is fine).

1 Add Rust to your PATH

The installer will add the following to your shell configuration:

source "$HOME/.cargo/env"
2 Restart your terminal or run:
source ~/.cargo/env

Windows

1 Download the installer

Visit rustup.rs and download rustup-init.exe

2 Install Visual C++ Build Tools

Rust requires the Microsoft C++ Build Tools. The installer will prompt you to install them if needed.

3 Run the installer and follow the prompts

Verifying the Installation

After installation, verify everything works:

# Check Rust compiler version
rustc --version
# Example output: rustc 1.75.0 (82e1608df 2023-12-21)

# Check Cargo version
cargo --version
# Example output: cargo 1.75.0 (1d8b05cdd 2023-11-20)

# Check rustup version
rustup --version
# Example output: rustup 1.26.0 (5af9b9484 2023-04-05)
Installed Tools

rustup installs these essential components:

2.2 Updating and Managing Rust

Rust releases a new stable version every six weeks. Keep your installation current:

# Update Rust to the latest version
rustup update

# Show installed toolchains
rustup show

# Install a specific version
rustup install 1.70.0

# Switch default toolchain
rustup default stable

# Install nightly (for experimental features)
rustup install nightly
rustup run nightly cargo build
Stable vs Nightly

Use stable for production code. The nightly channel has experimental features that may change. Some tools (like rocket web framework) may require nightly.

2.3 IDE Setup

A good IDE makes Rust development much more productive. We recommend Visual Studio Code with the rust-analyzer extension.

Visual Studio Code Setup

1 Install VS Code

Download from code.visualstudio.com

2 Install rust-analyzer extension

Search for "rust-analyzer" in the Extensions panel (Ctrl+Shift+X) and install it.

3 Recommended additional extensions:

What rust-analyzer Provides

Feature Description
Code completion Smart suggestions as you type
Go to definition Jump to function/type definitions (F12)
Inline errors See compiler errors without building
Inlay hints Show types and parameter names inline
Rename refactoring Safely rename across project
Code actions Quick fixes and code generation

Other IDE Options

2.4 Your First Rust Program

Let's create and run your first Rust program using Cargo.

Creating a New Project

# Create a new project called hello_rust
cargo new hello_rust

# Navigate into the project
cd hello_rust

Cargo creates this project structure:

Understanding Cargo.toml

Open Cargo.toml - this is your project's manifest:

[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at
# https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Your external dependencies go here
Key Cargo.toml Sections

The Main File

Open src/main.rs:

fn main() {
    println!("Hello, world!");
}

Let's break this down:

Code Explanation
fn Keyword to define a function
main() The entry point - every Rust program starts here
{ } Function body enclosed in curly braces
println! A macro (note the !) that prints to console
"Hello, world!" A string literal
; Statements end with semicolons

Running the Program

# Build and run
cargo run

# Output:
   Compiling hello_rust v0.1.0 (/path/to/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.42s
     Running `target/debug/hello_rust`
Hello, world!
Congratulations!

You've just written and run your first Rust program!

2.5 Understanding Cargo Commands

Cargo is your constant companion in Rust development. Here are the essential commands:

Command What It Does
cargo new <name> Create a new project
cargo build Compile the project
cargo run Build and run
cargo check Check for errors without building (faster)
cargo test Run tests
cargo fmt Format code
cargo clippy Run linter for code improvements
cargo doc --open Generate and open documentation
cargo build --release Build optimized release version
cargo add <crate> Add a dependency

Debug vs Release Builds

# Debug build (fast compilation, slow execution, with debug symbols)
cargo build
# Binary: target/debug/hello_rust

# Release build (slow compilation, fast execution, optimized)
cargo build --release
# Binary: target/release/hello_rust
When to Use Release Mode

Use cargo run during development for fast iteration. Use cargo build --release for performance testing and deployment. The performance difference can be 10-100x!

2.6 Adding Dependencies

Rust's package ecosystem lives on crates.io. Let's add a dependency:

# Add the rand crate
cargo add rand

This updates your Cargo.toml:

[dependencies]
rand = "0.8.5"

Now use it in your code:

use rand::Rng;

fn main() {
    let secret_number = rand::thread_rng().gen_range(1..=100);
    println!("Secret number: {}", secret_number);
}

Understanding Version Numbers

Syntax Meaning
"0.8.5" Compatible with 0.8.5+ but less than 0.9.0
"^0.8.5" Same as above (caret is default)
"~0.8.5" Patch updates only (0.8.5 - 0.8.x)
"=0.8.5" Exactly this version
"*" Any version (not recommended)

2.7 Project Structure Best Practices

As your project grows, organize it properly:

Library vs Binary Crates

# Create a library project
cargo new my_library --lib

# Create a binary project (default)
cargo new my_app
Binary vs Library

2.8 Chapter Summary

Key Takeaways

2.9 Review Questions

Test Your Understanding

  1. What is rustup and why is it preferable to installing Rust from package managers?
  2. What's the difference between cargo build and cargo check? When would you use each?
  3. Explain the purpose of the Cargo.toml file. What sections does it contain?
  4. What does the ! in println! indicate?
  5. What's the difference between debug and release builds? When should you use each?
  6. How do you add an external dependency to a Rust project? What file is modified?

2.10 Looking Ahead

What's Next: Chapter 3 - Ownership

In the next chapter, you'll learn Rust's most important concept—ownership. This is what makes Rust unique and enables memory safety without garbage collection:

Korea Standardization Infrastructure Mapping

Korea operates a comprehensive standards governance system through inter-ministerial cooperation. National Standards Council (under Prime Minister's Office, per Framework Act on National Standards Article 5) coordinates KATS (Korean Agency for Technology and Standards), MFDS (Ministry of Food and Drug Safety), MOTIE (Ministry of Trade, Industry and Energy), MSIT (Ministry of Science and ICT), MOIS (Ministry of the Interior and Safety), MOE (Ministry of Environment), MOHW (Ministry of Health and Welfare), MND (Ministry of National Defense), MCST (Ministry of Culture, Sports and Tourism), MOFA (Ministry of Foreign Affairs), MOJ (Ministry of Justice), and FSC (Financial Services Commission). Accreditation and Testing: KOLAS (Korea Laboratory Accreditation Scheme) accredits 800+ testing laboratories. KAS (Korea Accreditation System) accredits 50+ certification bodies. KTC (Korea Testing Certification), KTR (Korea Testing & Research Institute), KTL (Korea Testing Laboratory), and KCL (Korea Conformity Laboratories) provide conformance testing. Telecom and Cyber: KCC (Korea Communications Commission), KCA (Korea Communications Agency), TTA (Telecommunications Technology Association), IITP (Institute for Information & Communications Technology Planning & Evaluation), NIPA (National IT Industry Promotion Agency), KISA (Korea Internet & Security Agency), KCMVP (Korea Cryptographic Module Validation Program), NIS (National Intelligence Service), NSR (National Security Research Institute), and NCSC (National Cyber Security Center). National R&D Centers: KIST, ETRI, KAIST, Seoul National University, Yonsei University, Korea University, POSTECH, UNIST, GIST, DGIST, KISTI, KIER, KIMM, KRICT, KFRI, KRIBB. International Standards Cooperation: ISO TC/SC Korean secretariats, IEC TC/SC Korean secretariats, ITU-T Study Group Korean chairs, 3GPP RAN/SA Korean chairs, IEEE 802 Korean chairs, W3C Korea office, OASIS Korea office, IETF Korea cooperation, OECD CSTP, UN ESCAP, APEC SCSC Korean cooperation. Korean Industrial Standards (KS) Catalog: KS X (Information) 25,000+, KS A (Basic) 15,000+, KS B (Machinery) 25,000+, KS C (Electrical) 18,000+, KS D (Metallurgy) 12,000+, KS E (Mining) 5,000+, KS F (Construction) 18,000+, KS H (Food) 8,000+, KS I (Environment) 5,000+, KS J (Biology) 3,000+, KS K (Textile) 15,000+, KS L (Ceramics) 7,000+, KS M (Chemistry) 12,000+, KS P (Medical) 5,000+, KS Q (Quality Mgmt) 4,000+, KS R (Transport) 12,000+, KS S (Service) 3,000+, KS T (Packaging) 4,000+, KS V (Shipbuilding) 5,000+, KS W (Aerospace) 3,000+ — totaling 220,000+ Korean Industrial Standards. Key Acts: Personal Information Protection Act (Act 19234, effective Sept 15, 2024), Electronic Government Act, Electronic Signature Act, Act on Promotion of Information and Communications Network Utilization and Information Protection, Information and Communications Infrastructure Protection Act, Data Industry Act, Public Data Act, AI Framework Act (Act 20212, effective July 2026), Industrial Technology Innovation Promotion Act, Framework Act on Science and Technology — 70+ Korean standardization-related laws.

Korea Digital Transformation Detailed Mapping

Korea operates digital transformation through a comprehensive governance system. Digital Government: Digital Platform Government Committee (established September 2022, under the President)·Ministry of the Interior and Safety Digital Government Bureau·e-Government Support Center·Gov.kr·National Citizen Service·KDIS (Korea Digital Information Society)·NIA (National Information Society Agency)·MOIS (Ministry of the Interior and Safety). K-DNS Infrastructure: Korea Internet & Security Agency (KISA) Korea Internet Center·KISA DNS Root Server·KRNIC (Korea Network Information Center)·BGP Korea·National Cyber Security Center (NCSC)·KCC (Korea Communications Commission)·MSIT (Ministry of Science and ICT)·NIA·NIPA. Korean Cloud Infrastructure: KT Cloud·NAVER Cloud (NCloud)·Samsung SDS Cloud·LG U+ Cloud·NHN Cloud·Kakao Enterprise Cloud·SK Telecom Cloud·KISA Cloud Security Assurance Program (CSAP)·KCMVP-validated cloud·ISMS-P (Information Security & Personal Information Management System). Korean Security Certifications: KISA ISMS-P certification·KCMVP (Korean Cryptographic Module Validation Program)·NIS (National Intelligence Service) "National Cryptographic Technology Operation Standards"·NCSC "National Cyber Security Strategy 2024-2028"·CC (Common Criteria) Korean evaluation bodies·EAL4·EAL5·KS X ISO/IEC 15408·19790·24759 Korean Profile. Korean Data Standards: NIA AI Hub·National Data Standardization Committee·Statistics Korea (KOSTAT)·MyData 4 Designated Combination Specialists (Samsung SDS, KICI, KOSTAT, KFTC)·National Institute of Korean Language·National Law Information Center·National Spatial Information Platform·National Spatial Data Center·Korean Spatial Information Standards. Finance and Fintech Standards: FSC (Financial Services Commission)·FSS (Financial Supervisory Service)·FIU (Financial Intelligence Unit)·BOK (Bank of Korea)·FSEC (Financial Security Institute)·KFTC (Korea Financial Telecommunications)·KSD (Korea Securities Depository)·KRX (Korea Exchange) 8-agency cooperation. 5G/6G Communications Infrastructure: 5G subscribers 35 million (2024)·5G base stations 350,000·6G commercialization target 2028·5G dedicated networks 16 operators·6G Acceleration Council (MSIT, 2024). K-Content: KOCCA (Korea Creative Content Agency)·MCST (Ministry of Culture, Sports and Tourism)·KCA (Korea Communications Agency)·Korea Culture Information Service Agency·Korean Film Archive·Korea Publishing Industry Promotion Agency. Data 3 Acts (Personal Information Protection Act·Credit Information Act·Telecommunications Network Act, 2020 enforcement)·Data Industry Act (2021)·Public Data Act (2013)·AI Framework Act (2026)·Digital Platform Government Framework Act (2024 proposed) — Korea digital transformation core legislation.