Borrowing
ํจ์ ํธ์ถ์ ๊ฐ์ ์์ ๊ถ์ ์ด๋ํ๋ ๋์ ์ ํจ์๊ฐ ๊ฐ์ _๋น๋ ค_์ฌ ์ ์์ต๋๋ค:
Instead of transferring ownership when calling a function, you can let a function borrow the value:
#[derive(Debug)] struct Point(i32, i32); fn add(p1: &Point, p2: &Point) -> Point { Point(p1.0 + p2.0, p1.1 + p2.1) } fn main() { let p1 = Point(3, 4); let p2 = Point(10, 20); let p3 = add(&p1, &p2); println!("{p1:?} + {p2:?} = {p3:?}"); }
add
ํจ์๋ ๋ Point๊ฐ์ฒด ๊ฐ์ _๋น๋ ค_์ค์์ ์๋ก์ด Point๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.- ํธ์ถ์(main ํจ์)๋ ์ฌ์ ํ p1, p2์ ์์ ๊ถ์ ์ ์งํฉ๋๋ค.
- The
add
function borrows two points and returns a new point.- The caller retains ownership of the inputs.