Technology Encyclopedia Home >What are the data types in Rust?

What are the data types in Rust?

In Rust, there are several fundamental data types that can be categorized into scalar and compound types.

Scalar Types:

  1. Integers: Signed and unsigned integers with various bit sizes (e.g., i8, u16, i32, u64).
    • Example: let x: i32 = 5;
  2. Floating-point numbers: Represent real numbers with decimal points.
    • Example: let y: f64 = 3.14;
  3. Booleans: Represent truth values.
    • Example: let is_true: bool = true;
  4. Characters: Represent single Unicode scalar values.
    • Example: let c: char = 'A';

Compound Types:

  1. Arrays: Fixed-size sequences of elements of the same type.
    • Example: let array: [i32; 5] = [1, 2, 3, 4, 5];
  2. Tuples: Ordered collections of elements of possibly different types.
    • Example: let tuple: (i32, f64, char) = (1, 3.14, 'A');
  3. Structs: Custom data types that can group different kinds of data.
    • Example:
      struct Point {
          x: i32,
          y: i32,
      }
      let p = Point { x: 1, y: 2 };
      
  4. Enums: Types that can have a fixed set of values.
    • Example:
      enum Color {
          Red,
          Green,
          Blue,
      }
      let color = Color::Red;
      

Reference Types:

  1. References: Pointers to data without taking ownership.
    • Example: let reference = &x;
  2. Slices: References to a contiguous sequence of elements in an array or vector.
    • Example: let slice = &array[1..3];

Smart Pointers:

  1. Box: Allocate values on the heap.
    • Example: let boxed_value = Box::new(5);
  2. Rc: Reference-counted pointers for shared ownership.
    • Example: let rc_value = Rc::new(5);
  3. Arc: Atomically reference-counted pointers for multi-threaded shared ownership.
    • Example: let arc_value = Arc::new(5);

Rust's type system is designed to ensure memory safety and performance, with a strong emphasis on ownership and borrowing rules.

For handling large-scale data and computations in the cloud, Tencent Cloud offers services like Tencent Cloud Compute and Tencent Cloud Storage, which can be integrated with Rust applications to leverage cloud computing capabilities.