Lifetimes

๋นŒ๋ ค์˜จ ๊ฐ’์€ ์ˆ˜๋ช… ์„ ๊ฐ–์Šต๋‹ˆ๋‹ค:

  • ์ˆ˜๋ช…๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: add(p1: &Point, p2: &Point) -> Point.
  • ๋ฌผ๋ก  ๋ช…์‹œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: &'a Point, &'document str.
  • &'a Point ๋Š” as a๊ฐ€ ์œ ํšจํ•œ ๋™์•ˆ ๋นŒ๋ ค์˜จ Point ์ž…๋‹ˆ๋‹ค.
  • ์ˆ˜๋ช…๋Š” ํ•ญ์ƒ ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ถ”๋ก ๋ฉ๋‹ˆ๋‹ค.: ์ง์ ‘ ์ˆ˜๋ช…์„ ์„ค์ •ํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.
    • ์ˆ˜๋ช… ํ‘œ๊ธฐ(')์€ ์ œ์•ฝ์กฐ๊ฑด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์œ ํšจํ•œ ์†”๋ฃจ์…˜์ด ์žˆ๋Š”์ง€ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.

A borrowed value has a lifetime:

  • The lifetime can be elided: add(p1: &Point, p2: &Point) -> Point.
  • Lifetimes can also be explicit: &'a Point, &'document str.
  • Read &'a Point as โ€œa borrowed Point which is valid for at least the lifetime aโ€.
  • Lifetimes are always inferred by the compiler: you cannot assign a lifetime yourself.
  • Lifetime annotations create constraints; the compiler verifies that there is a valid solution.

์—ญ์ฃผ

  • ์œ ํšจํ•œ ์†”๋ฃจ์…˜์ด ์–ด๋–ค์˜๋ฏธ์ธ์ง€ ๋ชจํ˜ธํ•œ๋ฐ ์ผ๋‹จ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฆฌํ„ด๊ฐ’์€ ๋ชจ๋‘ ๊ฐ™์€ ์ˆ˜๋ช…์ด์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.