RRust By Example
beginnerBasics

Type Aliases

Use type aliases to create shorter names for complex types.

Type Aliases

Use type aliases to create shorter names for complex types.

Difficulty

Beginner

Code

rust
type Kilometers = i32;
type Thunk = Box<dyn Fn() + Send + 'static>;

fn main() {
    let distance: Kilometers = 5;
    println!("{} km", distance);

    let f: Thunk = Box::new(|| println!("called!"));
    f();
}

Explanation

This example demonstrates how to use type aliases in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

Browse more examples in the basics category to build a complete understanding of this topic.

More Basics Examples