Lifetimes
๋น๋ ค์จ ๊ฐ์ ์๋ช ์ ๊ฐ์ต๋๋ค:
- ์๋ช
๋ ์๋ตํ ์ ์์ต๋๋ค:
add(p1: &Point, p2: &Point) -> Point. - ๋ฌผ๋ก ๋ช
์ํ ์๋ ์์ต๋๋ค:
&'a Point,&'document str. &'a Point๋ asa๊ฐ ์ ํจํ ๋์ ๋น๋ ค์จ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 Pointas โa borrowedPointwhich is valid for at least the lifetimeaโ.- 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.
์ญ์ฃผ
- ์ ํจํ ์๋ฃจ์ ์ด ์ด๋ค์๋ฏธ์ธ์ง ๋ชจํธํ๋ฐ ์ผ๋จ ๋งค๊ฐ๋ณ์์ ๋ฆฌํด๊ฐ์ ๋ชจ๋ ๊ฐ์ ์๋ช ์ด์ด์ผ ํ๋ค๊ณ ํฉ๋๋ค.