๋น๋ฆผ
Rustโs borrow checker puts constraints on the ways you can borrow values. For a given value, at any time:
- You can have one or more shared references to the value, or
- You can have exactly one exclusive reference to the value.
fn main() { let mut a: i32 = 10; let b: &i32 = &a; { let c: &mut i32 = &mut a; *c = 20; } println!("a: {a}"); println!("b: {b}"); }
This slide should take about 10 minutes.
- ์ถฉ๋ํ๋ ์ฐธ์กฐ๊ฐ ๊ฐ์ ์ง์ ์ _์กด์ฌ_ํด์๋ ์ ๋ฉ๋๋ค. ์ฐธ์กฐ๊ฐ ์ญ์ฐธ์กฐ๋๋ ์์น๋ ์ค์ํ์ง ์์ต๋๋ค.
- ์ ์ฝ๋ ์ปดํ์ผ ๋์ง ์์ต๋๋ค. ์๋ํ๋ฉด
c
๋a
๋ฅผ ๊ฐ๋ณ ๋ณ์๋ก ๋น๋ ธ๊ณ , ์ด์ ๋์์b
๋a
๋ฅผ ๋ถ๋ณ ๋ณ์๋ก ๋น๋ ธ๊ธฐ ๋๋ฌธ์ ๋๋ค. b
์ ๋ํprintln!
๊ตฌ๋ถ์c
๊ฐ ์๋ ์ค์ฝํ ์์ผ๋ก ์ด๋ํ๋ฉด ์ปดํ์ผ์ด ๋ฉ๋๋ค.- ์ด๋ ๊ฒ ๋ฐ๊พธ๋ฉด, ์ปดํ์ผ๋ฌ๋
c
๊ฐa
๋ฅผ ๊ฐ๋ณ ๋ณ์๋ก ๋น๋ฆฌ๊ธฐ ์ ์๋งb
๊ฐ ์ฌ์ฉ๋๋ค๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค. ๋น๋ฆผ ๊ฒ์ฌ๊ธฐ์ ์ด๋ฌํ ๊ธฐ๋ฅ์ โnon-lexical lifetimeโ ์ด๋ผ๊ณ ํฉ๋๋ค. - The exclusive reference constraint is quite strong. Rust uses it to ensure that data races do not occur. Rust also relies on this constraint to optimize code. For example, a value behind a shared reference can be safely cached in a register for the lifetime of that reference.
- ๋น๋ฆผ ๊ฒ์ฌ๊ธฐ๋ ๊ตฌ์กฐ์ฒด์ ์ฌ๋ฌ ํ๋์ ๋ํ ๋ฐฐํ์ ์ฐธ์กฐ๋ฅผ ๋์์ ๊ฐ์ ธ์ค๋ ๋ฑ ์ฌ๋ฌ ์ผ๋ฐ์ ์ธ ํจํด์ ์์ฉํ๋๋ก ์ค๊ณ๋์์ต๋๋ค. ํ์ง๋ง ์ ๋๋ก โ์ธ์โํ์ง ๋ชปํด โ๋น๋ฆผ ๊ฒ์ฌ๊ธฐ์์ ์ถฉ๋โ์ด ๋ฐ์ํ๋ ๊ฒฝ์ฐ๋ ์์ต๋๋ค.