Sling Academy
Home/Rust/Rust primitive types cheat sheet

Rust primitive types cheat sheet

Last updated: January 02, 2025

Rust, a systems programming language, is known for its performance and safety. A crucial part of using Rust efficiently is understanding its primitive types, which include integers, floating-point numbers, booleans, characters, and unit types. These types are the building blocks of any Rust program, enabling developers to handle simple data efficiently before proceeding to more complex data structures.

Integer Types

Rust provides several integer types, differentiated by size and signedness. They are fixed-size, allowing you to choose based on memory constraints and performance requirements:

TypeSize
i88-bit signed integer
u88-bit unsigned integer
i1616-bit signed integer
u1616-bit unsigned integer
i3232-bit signed integer
u3232-bit unsigned integer
i6464-bit signed integer
u6464-bit unsigned integer
i128128-bit signed integer
u128128-bit unsigned integer
isizeSigned integer with size equal to the pointer size
usizeUnsigned integer with size equal to the pointer size

Here's an example of using various integer types in Rust:

fn main() {
    let small_num: i8 = -10;
    let large_num: u32 = 42000;
    let auto_num = 200usize; // Type inferred as usize

    println!("Small Integer: {}", small_num);
    println!("Large Integer: {}", large_num);
    println!("Auto Inferred Integer: {}", auto_num);
}

Floating-Point Types

Rust supports two floating-point types: f32 and f64, corresponding to 32-bit and 64-bit floats:

fn main() {
    let float_one: f32 = 3.14;
    let float_two: f64 = 2.718;

    println!("Float 32: {}", float_one);
    println!("Float 64: {}", float_two);
}

The Boolean Type

The bool type represents a value that can either be true or false. It's particularly useful in control flow:

fn main() {
    let is_rust_awesome: bool = true;
    let is_java_better: bool = false;

    if is_rust_awesome {
        println!("Rust is awesome!");
    }
    if is_java_better {
        println!("You might need to reconsider!");
    }
}

The Character Type

The char type in Rust is used to represent a single Unicode scalar value. This means it can represent more than just ASCII:

fn main() {
    let char_example: char = 'R';
    let heart_emoji: char = '❤';

    println!("Character: {}", char_example);
    println!("Emoji: {}", heart_emoji);
}

Unit Type

The unit type in Rust is essentially an empty type, represented as (), commonly used as a return type for functions that do not return a value. It signifies actions or results that have no meaningful value:

fn main() {
    let unit_value: () = ();
    println!("The unit value: {:?}", unit_value);
}

Conclusion

Understanding Rust's primitive types is essential for writing efficient, safe, and performant code. As a developer, choosing the right primitive type can significantly impact the memory usage and execution speed of your application. Rust's static typing, combined with its collection of primitive types, provides a robust foundation for building complex and powerful applications.

Next Article: What are Rust macros?

Previous Article: Rust "Hello World" example

Series: The basics of Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior