Rust는 메모리 안전성과 성능을 동시에 제공하는 시스템 프로그래밍 언어입니다. 가비지 컬렉터 없이도 메모리 안전을 보장하며, C/C++에 필적하는 성능을 제공합니다.
Rust is a systems programming language that provides both memory safety and performance. It guarantees memory safety without a garbage collector while delivering performance comparable to C/C++.
소유권과 빌림 Ownership and Borrowing
Rust의 핵심 개념인 소유권 시스템은 컴파일 타임에 메모리 안전을 보장합니다.
Rust's core concept, the ownership system, guarantees memory safety at compile time.
// 소유권 이동
let s1 = String::from("hello");
let s2 = s1; // s1의 소유권이 s2로 이동
// 참조 빌림
let s3 = String::from("world");
let len = calculate_length(&s3); // 빌림
println!("Length: {}", len);
}
// Ownership move
let s1 = String::from("hello");
let s2 = s1; // ownership of s1 moves to s2
// Reference borrowing
let s3 = String::from("world");
let len = calculate_length(&s3); // borrow
println!("Length: {}", len);
}
구조체와 트레이트 Structs and Traits
struct User {
username: String,
email: String,
active: bool,
}
// 트레이트 정의
trait Displayable {
fn display(&self) -> String;
}
// 트레이트 구현
impl Displayable for User {
fn display(&self) -> String {
format!("{} ({})", self.username, self.email)
}
}
struct User {
username: String,
email: String,
active: bool,
}
// Trait definition
trait Displayable {
fn display(&self) -> String;
}
// Trait implementation
impl Displayable for User {
fn display(&self) -> String {
format!("{} ({})", self.username, self.email)
}
}
에러 처리 Error Handling
Rust는 Result와 Option 열거형을 사용해 안전하게 에러를 처리합니다.
Rust uses Result and Option enums for safe error handling.
if b == 0.0 {
Err(String::from("0으로 나눌 수 없습니다"))
} else {
Ok(a / b)
}
}
// 사용
match divide(10.0, 2.0) {
Ok(result) => println!("결과: {}", result),
Err(e) => println!("에러: {}", e),
}
if b == 0.0 {
Err(String::from("Cannot divide by zero"))
} else {
Ok(a / b)
}
}
// Usage
match divide(10.0, 2.0) {
Ok(result) => println!("Result: {}", result),
Err(e) => println!("Error: {}", e),
}
🦀 Rust 타자 연습의 장점 🦀 Benefits of Rust Typing Practice
Rust의 문법은 처음엔 복잡해 보이지만, 반복 연습으로 익숙해지면 메모리 안전한 코드를 빠르게 작성할 수 있습니다.
특히 ->, ::, &mut 같은 특수 기호들을 손에 익히세요.
Rust syntax may seem complex at first, but with practice you can quickly write memory-safe code.
Focus on symbols like ->, ::, &mut.
