ํ—ˆ์ƒ(dangling) ์ฐธ์กฐ

๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ฐธ์กฐ๋ผ๊ณ ๋„ ํ•˜๋Š” ๋ฐฐํƒ€์  ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฐธ์กฐ๋˜๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํƒ€์ž…์€ &mut T์ž…๋‹ˆ๋‹ค.

fn main() {
    let mut point = (1, 2);
    let x_coord = &mut point.0;
    *x_coord = 20;
    println!("point: {point:?}");
}
This slide should take about 10 minutes.

ํ‚ค ํฌ์ธํŠธ:

  • โ€™๋ฐฐํƒ€์ โ€™์ด๋ž€ ๊ฐ’์— ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ ์ด ์ฐธ์กฐ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋™์‹œ์— ๋‹ค๋ฅธ ์ฐธ์กฐ(๊ณต์œ  ๋˜๋Š” ๋ฐฐํƒ€์ )๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๋ฐฐํƒ€์  ์ฐธ์กฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋™์•ˆ์—๋Š” ์ฐธ์กฐ๋œ ๊ฐ’์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. x_coord๊ฐ€ ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๋Š” ๋™์•ˆ &point.0์„ ๋งŒ๋“ค๊ฑฐ๋‚˜ point.0์„ ๋ณ€๊ฒฝํ•ด ๋ณด์„ธ์š”.

  • Be sure to note the difference between let mut x_coord: &i32 and let 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.