Rust Primitive Types Exploration

RustRustBeginner
Practice Now

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

Introduction

In this lab, you will learn about the various primitives provided by Rust, including scalar types such as signed and unsigned integers, floating-point numbers, Unicode scalar values, boolean values, and the unit type. You will also explore compound types like arrays and tuples, and learn how to annotate variables with their respective types or allow Rust to infer the types from context.

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/BasicConceptsGroup -.-> rust/mutable_variables("`Mutable Variables`") rust/DataTypesGroup -.-> rust/integer_types("`Integer Types`") rust/DataTypesGroup -.-> rust/floating_types("`Floating-point Types`") rust/DataTypesGroup -.-> rust/boolean_type("`Boolean Type`") 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-99191{{"`Rust Primitive Types Exploration`"}} rust/mutable_variables -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} rust/integer_types -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} rust/floating_types -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} rust/boolean_type -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} rust/function_syntax -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} rust/expressions_statements -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} rust/lifetime_specifiers -.-> lab-99191{{"`Rust Primitive Types Exploration`"}} end

Primitives

Rust provides access to a wide variety of primitives. A sample includes:

Scalar Types

  • Signed integers: i8, i16, i32, i64, i128 and isize (pointer size)
  • Unsigned integers: u8, u16, u32, u64, u128 and usize (pointer size)
  • Floating point: f32, f64
  • char Unicode scalar values like 'a', 'Îą' and '∞' (4 bytes each)
  • bool either true or false
  • The unit type (), whose only possible value is an empty tuple: ()

Despite the value of a unit type being a tuple, it is not considered a compound type because it does not contain multiple values.

Compound Types

  • Arrays like [1, 2, 3]
  • Tuples like (1, true)

Variables can always be type annotated. Numbers may additionally be annotated via a suffix or by default. Integers default to i32 and floats to f64. Note that Rust can also infer types from context.

fn main() {
    // Variables can be type annotated.
    let logical: bool = true;

    let a_float: f64 = 1.0;  // Regular annotation
    let an_integer   = 5i32; // Suffix annotation

    // Or a default will be used.
    let default_float   = 3.0; // `f64`
    let default_integer = 7;   // `i32`

    // A type can also be inferred from context.
    let mut inferred_type = 12; // Type i64 is inferred from another line.
    inferred_type = 4294967296i64;

    // A mutable variable's value can be changed.
    let mut mutable = 12; // Mutable `i32`
    mutable = 21;

    // Error! The type of a variable can't be changed.
    mutable = true;

    // Variables can be overwritten with shadowing.
    let mutable = true;
}

Summary

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

Other Rust Tutorials you may like