Penflip

Penflip - Write better with others

  • Loading...
  • Discover Projects
  • Help
  • Signup
  • Login
  • Welcome back!
    No account? Signup Close
    Ready to write better?
    Have an account? Login Close

sarojaba · Rust Doc Korean

Make Changes
46

5.20. 드랍 (Drop) - 100%

지금까지 traits을 다루었으므로 Rust 표준 라이브러리가 제공하는 특별한 trait인 <code>Drop</code>에 대하여 다루어봅시다. Drop trait은 값이 scope를 벗어났을 때 일부 코드를 실행시키는 방법을 제공합니다. 예제:

struct HasDrop;

impl Drop for HasDrop {
    fn drop(&mut self) {
        println!("Dropping!");
    }
}

fn main() {
    let x = HasDrop;

    // do stuff

} // x goes out of scope here

x가 main()의 마지막에서 scope를 벗어났을 때, Drop에 대한 코드가 실행됩니다. Drop은 drop() 메서드를 하나 가지고 있습니다. 이는 &mut self를 인자로 받습니다.

이게 전부입니다! Drop의 작동 원리는 매우 간단합니다만, 약간의 중요한 요소가 남아있습니다. 예로 선언된 순서의 역순으로 값들은 드랍됩니다. 여기 또다른 예제가 있습니다.:

struct Firework {
    strength: i32,
}

impl Drop for Firework {
    fn drop(&mut self) {
        println!("BOOM times {}!!!", self.strength);
    }
}

fn main() {
    let firecracker = Firework { strength: 1 };
    let tnt = Firework { strength: 100 };
}

결과:

BOOM times 100!!!
BOOM times 1!!!

폭죽(firecracker)가 터지기 전에 TNT가 먼저 터집니다. 왜냐하면 나중에 선언되었기 때문입니다. 즉 Last in, first out: 나중에 들어온 것이, 먼저 나갑니다.

그렇다면 Drop은 어디에 사용될까요? 일반적으로, Drop은 struct와 관련된 자원을 청소할 때 사용됩니다. 예로 <code>Arc<T></code> type는 참조를 헤아리는 타입입니다. Drop이 실행될 때, <code>Arc<T></code>는 참조의 수를 감소시킵니다. 만약 참조의 수가 0에 도달한다면, <code>Arc<T></code>는 내부의 값을 청소할 것입니다.

Updated by sarojaba almost 3 years (view history)
5.19. 트레잇 (Traits) - 100% 5.21. if let - 100%

Contents

    Rust 문서 한글화 프로젝트 1. 소개(Introduction) 2. 시작하기(Getting Started) - 5% 3. Rust 배우기 (Learn Rust) - 100% 3.1. 추리 게임 (Guessing Game) - 100% 3.2. 식사하는 철학자들 (Dining Philosophers) - 100% 3.3. 다른 언어에 Rust 포함하기 (Rust Inside Other Languages) - 100% 4. 효과적인 Rust (Effective Rust) - 100% 4.1. 스택과 힙 (The Stack and the Heap) - 100% 4.2. 테스팅 (Testing) - 95% 4.3. 조건부 컴파일 (Conditional Compilation) - 70% 4.4. 문서화 (Documentation) - 20% 4.5. 반복자 (Iterators) - 100% 4.6. 동시성 (Concurrency) - 90% 4.7. 오류 처리 (Error Handling) - 0% 4.8. Choosing your Guarantees - 0% 4.9. 외부 함수 인터페이스 (Foreign Function Interface) - 50% 4.9. Borrow 와 AsRef 4.11. 배포 채널 (Release Channels) - 80% 5. 문법과 의미 (Syntax and Semantics) - 100% 5.1. 변수 바인딩 (Variable Bindings) - 100% 5.2. 함수 (Functions) - 100% 5.3. 기본형 (Primitive Types) - 100% 5.4. 주석 (Comments) - 100% 5.5. 조건식 (if) - 100% 5.6. 반복 (Loops) - 100% 5.7. 소유권 (Ownership) - 100% 5.8. 참조와 빌림 (References and Borrowing) - 100% 5.9. 수명 (Lifetimes) - 90% 5.10. 가변성 (Mutability) - 99% 5.11. 구조체 (Structs) - 100% 5.12. 열거형 (Enums) - 100% 5.13. 정합 (Match) - 100% 5.14. 패턴 (Patterns) - 80% 5.15. 메소드 문법 (Method Syntax) - 100% 5.16. 벡터 (Vectors) - 100% 5.17. 문자열(Strings) - 100% 5.18. 제너릭 (Generics) - 100% 5.19. 트레잇 (Traits) - 100% 5.20. 드랍 (Drop) - 100% 5.21. if let - 100% 5.22. 트레잇 객체 (Trait Objects) - 75% 5.23. 클로저 (Closures) - 10% 5.24. Universal Function Call Syntax 5.25. Crates and Modules 9. 용어 색인 (Concordance) Show all
    Discussions 5 Pending changes 5 Contributors
    Download Share

    Download

    Working...

    Downloading...

    Downloaded!

    Download more

    Error!

    Your download couldn't be processed. Check for abnormalities and incorrect syntax. We've been notified of the issue.

    Back

    Download PDF Download ePub Download HTML Download Word doc Download text Download source (archive)

    Close
188 Words
1,243 Characters

Share

Collaborators make changes on their own versions, and all changes will be approved before merged into the main version.

Close

Penflip is made by Loren Burton
in Los Angeles, California

Tweet

    About

  • Team
  • Pricing
  • Our Story

    Quick Start

  • Markdown
  • Penflip Basics
  • Working Offline

    Support

  • Help
  • Feedback
  • Terms & Privacy

    Connect

  • Email
  • Twitter