Borrowing
Borrowing
Rust's reference system. Borrows a value for use without transferring ownership. Divided into immutable and mutable references.
Borrowing
Rust's reference system. Borrows a value for use without transferring ownership. Divided into immutable and mutable references.
Borrowing is a core memory management concept of the Rust programming language, a way of accessing data through references without transferring ownership of the value. It is similar to pointers in C/C++, but safety is guaranteed at compile time. Borrowing allows the same data to be used efficiently across multiple code blocks.
Borrowing has two types: immutable references (&T) and mutable references (&mut T). Multiple immutable references can exist at the same time but cannot modify the data, while only one mutable reference can exist and it can modify the data. This rule prevents data race conditions at the root.
The core rule of borrowing is "multiple immutable references or one mutable reference." That is, an immutable reference and a mutable reference cannot exist at the same time, and the compiler strictly checks this. Thanks to this rule, safe code can be written even in multithreaded environments.
The lifetime of a reference is also an important concept in borrowing. A reference cannot live longer than the original data, and the Borrow Checker verifies this automatically. There are also cases where lifetime annotations must be specified explicitly when using references in function parameters or return values.
// Rust 보로잉 예제
fn main() {
let mut s = String::from("hello");
// 불변 참조 (여러 개 가능)
let r1 = &s;
let r2 = &s;
println!("불변 참조들: {}, {}", r1, r2);
// 가변 참조 (하나만 가능)
let r3 = &mut s;
r3.push_str(", world");
println!("가변 참조: {}", r3);
// 함수에 참조 전달
let len = calculate_length(&s);
println!("'{}'의 길이: {}", s, len);
// 가변 참조로 수정
change(&mut s);
println!("수정 후: {}", s);
}
// 불변 참조를 받아 읽기만 함
fn calculate_length(s: &String) -> usize {
s.len()
}
// 가변 참조를 받아 수정
fn change(s: &mut String) {
s.push_str("!");
}
Tech Lead: "A lot of data copying is happening in this logic. I'm worried about performance."
Developer: "If we use borrowing and pass by reference instead of copying, we can reduce memory allocation."
Tech Lead: "Are there parts that need a mutable reference?"
Developer: "Yes, in the update logic we pass it as &mut, and where we only read, we can use an immutable reference."
Interviewer: "Please explain Rust's borrowing rules."
Candidate: "Borrowing is a way of accessing a value without transferring ownership. Multiple immutable references are allowed, but only one mutable reference can exist at a time."
Interviewer: "Why is there such a restriction?"
Candidate: "It is to prevent data races at compile time. It guarantees that data is either read from many places at once or written from only one place, which enables safe concurrency."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: "You're calling clone() here, and it looks like an unnecessary copy."
Author: "It wouldn't compile because of the borrowing rules, so I put in a clone temporarily."
Reviewer: "I think adjusting the scope of the reference would solve it. You can create the mutable reference after the immutable reference is no longer used."
Author: "Ah, NLL (Non-Lexical Lifetimes) makes that possible. I'll fix it."