Move Semantics
(๋ณ์์)ํ ๋น์ ๋ณ์ ๊ฐ ์์ ๊ถ์ ์ด๋์ํต๋๋ค:
An assignment will transfer ownership between variables:
fn main() { let s1: String = String::from("Hello!"); let s2: String = s1; println!("s2: {s2}"); // println!("s1: {s1}"); }
- s1์ s2์ ํ ๋นํ์ฌ ์์ ๊ถ์ ์ด์ ์ํต๋๋ค.
- ๋ฐ์ดํฐ๋ s1์์ _์ด๋_๋ฉ๋๋ค. ๋ฐ๋ผ์ s1์ ๋์ด์ ์ ๊ทผ ํ ์ ์์ต๋๋ค.
s1
์ ์ค์ฝํ๊ฐ ์ข ๋ฃ๋๋ฉด ์๋ฌด ์ผ๋ ์์ต๋๋ค: ์๋ฌด๋ฐ ์์ ๊ถ์ด ์์ต๋๋ค.s2
์ ์ค์ฝํ๊ฐ ์ข ๋ฃ๋๋ฉด ๋ฌธ์์ด ๋ฐ์ดํฐ๋ ํด์ ๋ฉ๋๋ค.- ๊ฐ(๋ฐ์ดํฐ)์ ์์ ๊ถ์ ๊ฐ๋ ๋ณ์๋ ํญ์ ์ ํํ ํ๋ ์ ๋๋ค.
- The assignment of
s1
tos2
transfers ownership.- The data was moved from
s1
ands1
is no longer accessible.- When
s1
goes out of scope, nothing happens: it has no ownership.- When
s2
goes out of scope, the string data is freed.- There is always exactly one variable binding which owns a value.