Aliasing - Rust By Example
Description: Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

## 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

# Aliasing

The `type` statement can be used to give a new name to an existing type. Types must have `UpperCamelCase` names, or the compiler will raise a warning. The exception to this rule are the primitive types: `usize`, `f32`, etc.

    // `NanoSecond`, `Inch`, and `U64` are new names for `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;

        // Note that type aliases *don't* provide any extra type safety, because
        // aliases are *not* new types
        println!("{} nanoseconds + {} inches = {} unit?",
                 nanoseconds,
                 inches,
                 nanoseconds + inches);
    }

The main use of aliases is to reduce boilerplate; for example the `io::Result<T>` type is an alias for the `Result<T, io::Error>` type.

### Ver también

Attributes