[2026] Getting Started with Rust | Memory-Safe Systems Programming

[2026] Getting Started with Rust | Memory-Safe Systems Programming

이 글의 핵심

Rust tutorial for beginners: memory-safe systems programming, Hello World, Cargo workflow, ownership basics, syntax, and a hands-on calculator—clear and practical.

Introduction

What is Rust?

Rust is a memory-safe systems programming language developed by Mozilla (now stewarded by the Rust Project). Highlights:

  • Memory safety: enforced at compile time
  • Zero-cost abstractions: no extra runtime overhead for idiomatic code
  • Fearless concurrency: data-race freedom checked by the compiler
  • Performance: on par with C/C++
  • Tooling: Cargo for builds and dependencies Rust vs C++: | Aspect | Rust | C++ | |--------|------|-----| | Memory safety | Compile time (by default) | Runtime (optional tooling) | | Null pointers | Avoided with Option | Possible | | Package management | Cargo | CMake, vcpkg, Conan, etc. | | Learning curve | Steep | Very steep |

1. Installation

Installing rustup

Windows:

  1. Download from rustup.rs
  2. Run the installer Mac/Linux:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Verify the install

rustc --version
cargo --version

2. Hello World

Create a project

cargo new hello_rust
cd hello_rust

src/main.rs

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

Run

cargo run

3. Cargo

Project layout

아래 코드는 code를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

hello_rust/
├── Cargo.toml
├── Cargo.lock
└── src/
    └── main.rs

Cargo.toml

아래 코드는 toml를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

Common commands

아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

cargo new project_name    # New project
cargo build              # Build
cargo run                # Build and run
cargo test               # Run tests
cargo check              # Fast typecheck
cargo build --release    # Release build

4. Basic syntax

Variables

아래 코드는 rust를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

fn main() {
    // Immutable by default
    let x = 5;
    // x = 6;  // Error!
    
    // Mutable
    let mut y = 5;
    y = 6;  // OK
    
    // Explicit type
    let z: i32 = 10;
}

Functions

아래 코드는 rust를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

fn add(a: i32, b: i32) -> i32 {
    a + b  // `return` can be omitted
}
fn main() {
    let result = add(10, 20);
    println!("result: {}", result);
}

5. Ownership

Core idea

Rust’s ownership model is central to memory safety: 아래 코드는 rust를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

fn main() {
    // String::from allocates on the heap
    let s1 = String::from("hello");
    
    // Ownership moves from s1 to s2
    // s1 is invalidated and must not be used
    let s2 = s1;
    
    // println!("{}", s1);  // Compile error!
    // "value borrowed here after move"
    // s1 no longer owns the data
    
    println!("{}", s2);  // OK — s2 owns the string
}

Why?

  • In C++, after s2 = s1 both names may still be valid → risk of double free
  • Rust moves ownership so only one owner exists → safe deallocation

References (borrowing)

Use a reference to borrow without transferring ownership: 다음은 rust를 활용한 상세한 구현 코드입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

fn main() {
    let s1 = String::from("hello");
    
    // &s1: immutable borrow; s1 still owns the data
    let len = calculate_length(&s1);
    
    // s1 is still valid
    println!("{} length: {}", s1, len);
}
fn calculate_length(s: &String) -> usize {
    // s is a reference only; no ownership
    // Dropping s does not free the heap data
    s.len()
}

Borrowing rules:

  • Any number of immutable references (&T) at once (read-only)
  • At most one mutable reference (&mut T) at a time (exclusive write)
  • Immutable and mutable borrows cannot overlap

Mutable references

To mutate through a borrow, use &mut: 아래 코드는 rust를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

fn main() {
    let mut s = String::from("hello");
    
    change(&mut s);
    
    println!("{}", s);  // hello, world
}
fn change(s: &mut String) {
    s.push_str(", world");
}

Mutable borrow restrictions: 아래 코드는 rust를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

let mut s = String::from("hello");
let r1 = &mut s;
// let r2 = &mut s;  // Error!
// "cannot borrow `s` as mutable more than once at a time"
// Only one active mutable borrow → no data races
r1.push_str(" world");

What the compiler gives you:

  • No overlapping mutable borrows → no data races
  • While references exist, the owner cannot invalidate them → no dangling pointers
  • All checked at compile time → no runtime GC for these guarantees

6. Data types

Scalar types

다음은 rust를 활용한 상세한 구현 코드입니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

// Integers
let a: i8 = 127;
let b: i32 = 2147483647;
let c: u32 = 4294967295;
// Floats
let x: f32 = 3.14;
let y: f64 = 3.14159;
// Boolean
let t: bool = true;
let f: bool = false;
// char (Unicode scalar)
let c: char = 'A';
let emoji: char = '😀';

Compound types

아래 코드는 rust를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

// Tuple
let tup: (i32, f64, char) = (500, 6.4, 'A');
let (x, y, z) = tup;
println!("{}, {}, {}", x, y, z);
// Array (fixed size, stack)
let arr = [1, 2, 3, 4, 5];
let first = arr[0];

7. Hands-on example

Mini calculator

다음은 rust를 활용한 상세한 구현 코드입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

fn main() {
    println!("=== Calculator ===");
    
    let a = 10;
    let b = 5;
    
    println!("{} + {} = {}", a, b, add(a, b));
    println!("{} - {} = {}", a, b, subtract(a, b));
    println!("{} * {} = {}", a, b, multiply(a, b));
    println!("{} / {} = {}", a, b, divide(a, b));
}
fn add(a: i32, b: i32) -> i32 { a + b }
fn subtract(a: i32, b: i32) -> i32 { a - b }
fn multiply(a: i32, b: i32) -> i32 { a * b }
fn divide(a: i32, b: i32) -> i32 { a / b }

Summary

Takeaways

  1. Rust: memory-safe systems language with strong tooling
  2. Cargo: build tool and package manager
  3. Ownership: foundation of memory and thread safety
  4. Immutability: default for bindings (let vs let mut)
  5. Performance: comparable to C/C++ when optimized

Next steps


... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3