ํ์(dangling) ์ฐธ์กฐ
๋ณ๊ฒฝ ๊ฐ๋ฅํ ์ฐธ์กฐ๋ผ๊ณ ๋ ํ๋ ๋ฐฐํ์ ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์ฐธ์กฐ๋๋ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ํ์
์ &mut T
์
๋๋ค.
fn main() { let mut point = (1, 2); let x_coord = &mut point.0; *x_coord = 20; println!("point: {point:?}"); }
ํค ํฌ์ธํธ:
-
โ๋ฐฐํ์ โ์ด๋ ๊ฐ์ ์ก์ธ์คํ๋ ๋ฐ ์ด ์ฐธ์กฐ๋ง ์ฌ์ฉํ ์ ์์์ ์๋ฏธํฉ๋๋ค. ๋์์ ๋ค๋ฅธ ์ฐธ์กฐ(๊ณต์ ๋๋ ๋ฐฐํ์ )๊ฐ ์กด์ฌํ ์ ์์ผ๋ฉฐ, ๋ฐฐํ์ ์ฐธ์กฐ๊ฐ ์กด์ฌํ๋ ๋์์๋ ์ฐธ์กฐ๋ ๊ฐ์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
x_coord
๊ฐ ํ์ฑํ๋์ด ์๋ ๋์&point.0
์ ๋ง๋ค๊ฑฐ๋point.0
์ ๋ณ๊ฒฝํด ๋ณด์ธ์. -
Be sure to note the difference between
let mut x_coord: &i32
andlet x_coord: &mut i32
. The first one represents a shared reference which can be bound to different values, while the second represents an exclusive reference to a mutable value.