Drop ํธ๋ ์
Dropํธ๋ ์์ ๊ตฌํํ๋ฉด, ๊ทธ ๊ฐ์ด ์ค์ฝํ ๋ฐ์ผ๋ก ๋๊ฐ ๋ ์คํ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค:
struct Droppable { name: &'static str, } impl Drop for Droppable { fn drop(&mut self) { println!("Dropping {}", self.name); } } fn main() { let a = Droppable { name: "a" }; { let b = Droppable { name: "b" }; { let c = Droppable { name: "c" }; let d = Droppable { name: "d" }; println!("Exiting block B"); } println!("Exiting block A"); } drop(a); println!("Exiting main"); }
dropis called automatically, but it can be called manually like in this example.- If it was called manually, it wonโt be called at the end of the scope for the second time.
- Calling
dropcan be useful for objects that do some work ondrop: releasing locks, closing files, etc.
๋ ผ์์ :
Drop::drop์ ์self๋ฅผ ์ธ์๋ก ๋ฐ์ง ์์ต๋๊น?- ์งง์ ๋๋ต: ๋ง์ฝ ๊ทธ๋ ๊ฒ ๋๋ค๋ฉด
std::mem::drop์ด ๋ธ๋ก์ ๋์์ ํธ์ถ๋๊ณ , ๋ค์Drop::drop์ ํธ์ถํ๊ฒ ๋์ด, ์คํ ์ค๋ฒํ๋ก๊ฐ ๋ฐ์ํฉ๋๋ค!
- ์งง์ ๋๋ต: ๋ง์ฝ ๊ทธ๋ ๊ฒ ๋๋ค๋ฉด
drop(a)๋ฅผa.drop()๋ก ๋ณ๊ฒฝํด ๋ณด์๊ธฐ ๋ฐ๋๋๋ค.