Ownership

๋ชจ๋“  ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ์€ ์œ ํšจํ•œ _๋ฒ”์œ„_๋ฅผ ๊ฐ–์œผ๋ฉฐ, ๋ฒ”์œ„ ๋ฐ–์—์„œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์€ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค:

All variable bindings have a scope where they are valid and it is an error to use a variable outside its scope:

struct Point(i32, i32);

fn main() {
    {
        let p = Point(3, 4);
        println!("x: {}", p.0);
    } // ์Šค์ฝ”ํ”„ ์ข…๋ฃŒ์ง€์ 
    println!("y: {}", p.1);
} // main ํ•จ์ˆ˜์˜ ์Šค์ฝ”ํ”„ ์ข…๋ฃŒ์ง€์ 
  • ์Šค์ฝ”ํ”„๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๋ณ€์ˆ˜๋Š” _์‚ญ์ œ_๋˜๊ณ  ๋ฐ์ดํ„ฐ ๋ฉ”๋ชจ๋ฆฌ๋Š” ํ•ด์ œ๋ฉ๋‹ˆ๋‹ค.
  • ์†Œ๋ฉธ์ž๋Š” ์—ฌ๊ธฐ(์Šค์ฝ”ํ”„ ์ข…๋ฃŒ์ง€์ )์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์ž์›์„ ํ•ด์ œ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๊ฒƒ์„ ๋‘๊ณ  ๋ณ€์ˆ˜๊ฐ€ ๊ฐ’์„ _์†Œ์œ _ํ•œ๋‹ค๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • At the end of the scope, the variable is dropped and the data is freed.
  • A destructor can run here to free up resources.
  • We say that the variable owns the value.