Ownership
Ownership
A core concept in Rust. Each value has exactly one owner. When the owner goes out of scope, the memory is freed.
Ownership
A core concept in Rust. Each value has exactly one owner. When the owner goes out of scope, the memory is freed.
Ownership is the most central concept of the Rust programming language, a unique system that guarantees memory safety without a garbage collector. Every value has exactly one owner, and when the owner goes out of scope, the value is automatically freed. This fundamentally prevents memory leaks and double frees.
Ownership has three core rules. First, each value has a variable called its owner. Second, there can only be one owner at a time. Third, when the owner goes out of scope, the value is dropped. These simple rules solve complex memory management problems at compile time.
An ownership transfer (move) occurs when a value is assigned to another variable or passed to a function. For data allocated on the heap (String, Vec, etc.), the original variable is invalidated when moved. In contrast, simple types stored on the Stack (i32, bool, etc.) implement the Copy Trait, so their values are copied.
The Ownership system works together with Borrowing to provide flexibility. Through references (&T, &mut T), a value can be accessed without transferring ownership, and the Borrow Checker verifies this at compile time. This mechanism is the foundation of Rust's "zero-cost abstractions" and safe concurrency.
// Rust 소유권 예제
fn main() {
// 소유권 생성
let s1 = String::from("hello");
// 소유권 이전 (move)
let s2 = s1;
// println!("{}", s1); // 오류! s1은 더 이상 유효하지 않음
println!("{}", s2); // OK
// 스택 타입은 복사됨 (Copy 트레이트)
let x = 5;
let y = x;
println!("x = {}, y = {}", x, y); // 둘 다 유효
// 함수에 소유권 전달
let s3 = String::from("world");
takes_ownership(s3);
// println!("{}", s3); // 오류! 소유권이 함수로 이동됨
// 소유권 반환받기
let s4 = gives_ownership();
println!("받은 문자열: {}", s4);
// Clone으로 깊은 복사
let s5 = String::from("clone me");
let s6 = s5.clone();
println!("s5 = {}, s6 = {}", s5, s6); // 둘 다 유효
}
fn takes_ownership(s: String) {
println!("소유권 받음: {}", s);
} // s가 drop됨
fn gives_ownership() -> String {
String::from("returned") // 소유권 반환
}
Developer A: "This function receives a Vec and processes it, but the caller also needs to keep using it, so I'm using clone."
Developer B: "If you don't need to take ownership, wouldn't it be better to receive it as a reference (&Vec)? You can reduce unnecessary copying."
Developer A: "You're right. Since it only needs to be read, an immutable reference would be enough."
Developer B: "Yes, receive by value only when an ownership transfer is needed, and use Borrowing for the rest."
Interviewer: "How does Rust's Ownership system guarantee memory safety?"
Candidate: "According to the ownership rules, each value has only one owner, and when the owner goes out of scope, it is automatically freed. This makes memory leaks and double frees impossible."
Interviewer: "What is the difference from a garbage collector?"
Candidate: "GC tracks memory at runtime, but Rust decides memory management at compile time. Therefore, there is no runtime overhead, and the timing of memory release is predictable."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: "Here you receive a String and immediately pass it to another function, but if you try to use it again from the original, you'll get a compile error."
Author: "Ah, that's because the ownership was moved. What should I do?"
Reviewer: "There are two ways. Pass a reference, or have the function return ownership after processing."
Author: "Since no modification is needed, I'll receive it as a &str slice. It's also more flexible."