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.7. while 반복문 (while Loops) - 100%

러스트에는 while 반복문 역시 존재합니다. 다음과 같은 모습이죠:

let mut x = 5; // mut x: i32
let mut done = false; // mut done: bool

while !done {
    x += x - 3;

    println!("{}", x);

    if x % 5 == 0 {
        done = true;
    }
}

while 반복문은 당신이 얼마나 많은 횟수를 반복해야할지 확신이 없을 때 좋은 선택지입니다.

무한 루프가 필요할 때, 다음과 같이 쓰고 싶은 마음이 들겠죠:

while true {

하지만, 러스트는 이 경우를 위한 예약어 loop를 가지고 있습니다:

loop {

이렇게 생성된 반복문은 항상 반복할 것이기 때문에, 러스트의 제어흐름 분석은 이러한 반복문을 while true와 다르게 취급합니다. 일반적으로, 우리가 컴파일러에게 더 많은 정보를 줄수록 컴파일러는 더 나은 보안과 최적화를 할 수 있기 때문에, 만약 무한 루프를 만들 생각이라면 loop 키워드를 사용하는게 좋습니다.

반복을 이르게 끝내기

아까 전 보았던 while 반복문을 다시 한 번 살펴보죠:

let mut x = 5;
let mut done = false;

while !done {
    x += x - 3;

    println!("{}", x);

    if x % 5 == 0 {
        done = true;
    }
}

보시다시피, 언제 반복문을 빠져나가야 할 지 알기 위해서 mut 불리언 변수 바인딩인 done이 계속 유지되어야 합니다. 러스트는 반복을 조작하기 위한 두 가지 키워드를 제공합니다: break와 continue죠.

이 경우, 우리는 break를 이용해 반복문을 보다 나은 방식으로 작성할 수 있습니다:

let mut x = 5;

loop {
    x += x - 3;

    println!("{}", x);

    if x % 5 == 0 { break; }
}

loop를 이용해 무한 반복문을 만든 뒤 break를 이용해 빠져나오는거죠.

continue 역시 비슷한 일을 하지만, 반복문을 끝내버리는 대신 다음 반복으로 넘어갑니다. 예를 들어, 아래의 코드는 홀수만 출력하죠:

for x in 0..10 {
    if x % 2 == 0 { continue; }

    println!("{}", x);
}

continue와 break는 둘 다 while 반복문과 for 반복문 내부에서 사용 가능합니다.

Updated by sarojaba about 4 years (view history)
5.6. for 반복문 (for Loops) - 100% 5.8. 소유권 (Ownership) - 100%

Contents

    Rust 문서 한글화 프로젝트 1. 소개(Introduction) - 95% 2. 시작하기(Getting Started) - 100% 2.1. Rust 설치하기 (Installing Rust) - 95% 2.2. 안녕, 세상아! (Hello, world!) - 100% 2.3. 안녕, 카고! (Hello, Cargo!) - 100% 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) - 100% 4.7. 오류 처리 (Error Handling) - 0% 4.8. Choosing your Guarantees - 0% 4.9. 외부 함수 인터페이스 (Foreign Function Interface) - 50% 4.10. Borrow 와 AsRef - 5% 4.11. 배포 채널 (Release Channels) - 80% 5. 문법과 의미 (Syntax and Semantics) - 100% 5.1. 변수 바인딩 (Variable Bindings) - 100% 5.2. 함수 (Functions) - 90% 5.3. 기본형 (Primitive Types) - 100% 5.4. 주석 (Comments) - 80% 5.5. 조건식 (if) - 100% 5.6. 반복 (Loops) - 50% 5.7. while 반복문 (while Loops) - 100% 5.8. 소유권 (Ownership) - 100% 5.9. 참조와 빌림 (References and Borrowing) - 100% 5.10. 수명 (Lifetimes) - 100% 5.11. 가변성 (Mutability) - 99% 5.12. 구조체 (Structs) - 100% 5.13. 열거형 (Enums) - 0% 5.14. 정합 (Match) - 99% 5.15. 패턴 (Patterns) - 100% 5.16. 메소드 문법 (Method Syntax) - 100% 5.17. 벡터 (Vectors) - 100% 5.18. 문자열(Strings) - 100% 5.19. 제너릭 (Generics) - 100% 5.20. 트레잇 (Traits) - 100% 5.21. 드랍 (Drop) - 100% 5.22. if let - 100% 5.23. 트레잇 객체 (Trait Objects) 5.24. 클로저 (Closures) - 10% 5.25. Universal Function Call Syntax 5.26. 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
259 Words
1,368 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