Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

별칭

type 문을 사용하면 기존 타입에 새로운 이름을 부여할 수 있습니다. 타입 이름은 UpperCamelCase여야 하며, 그렇지 않으면 컴파일러가 경고를 보냅니다. 이 규칙의 예외는 기본 타입인 usize, f32 등입니다.

// `NanoSecond`, `Inch`, `U64`는 `u64`를 가리키는 새로운 이름입니다.
type NanoSecond = u64;
type Inch = u64;
type U64 = u64;

fn main() {
    // `NanoSecond` = `Inch` = `U64` = `u64`입니다.
    let nanoseconds: NanoSecond = 5 as u64;
    let inches: Inch = 2 as U64;

    // 타입 별칭은 새로운 타입이 아니기 때문에, 추가적인 타입 안전성을
    // 제공하지 않는다는 점에 유의하세요.
    println!("{} 나노초 + {} 인치 = {} 유닛?",
             nanoseconds,
             inches,
             nanoseconds + inches);
}

별칭의 주요 용도는 보일러플레이트(boilerplate)를 줄이는 것입니다. 예를 들어 io::Result<T> 타입은 Result<T, io::Error> 타입의 별칭입니다.

참고:

속성