Rust Aliasing: Enhancing Code Readability

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we learn about aliasing in Rust, which allows us to give a new name to an existing type using the type statement. Aliases must follow certain naming conventions, and can be used to create new names for primitive types or user-defined types. The main purpose of aliases is to reduce code duplication and improve readability.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/BasicConceptsGroup(["`Basic Concepts`"]) rust(("`Rust`")) -.-> rust/DataTypesGroup(["`Data Types`"]) rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/MemorySafetyandManagementGroup(["`Memory Safety and Management`"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("`Variable Declarations`") rust/DataTypesGroup -.-> rust/integer_types("`Integer Types`") rust/DataTypesGroup -.-> rust/type_casting("`Type Conversion and Casting`") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/MemorySafetyandManagementGroup -.-> rust/lifetime_specifiers("`Lifetime Specifiers`") subgraph Lab Skills rust/variable_declarations -.-> lab-99298{{"`Rust Aliasing: Enhancing Code Readability`"}} rust/integer_types -.-> lab-99298{{"`Rust Aliasing: Enhancing Code Readability`"}} rust/type_casting -.-> lab-99298{{"`Rust Aliasing: Enhancing Code Readability`"}} rust/function_syntax -.-> lab-99298{{"`Rust Aliasing: Enhancing Code Readability`"}} rust/expressions_statements -.-> lab-99298{{"`Rust Aliasing: Enhancing Code Readability`"}} rust/lifetime_specifiers -.-> lab-99298{{"`Rust Aliasing: Enhancing Code Readability`"}} end

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.

Summary

Congratulations! You have completed the Aliasing lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like