Rust Data Structure

Basic Data Type

Primitive

  • 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
  • unit type (), whose only possible value is an empty tuple: ()

Compound

  • arrays like [1, 2, 3]
  • tuples like (1, true)

Defining and Instantiating Structs -> Class

User Structs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// Usage
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};

user1.email = String::from("anotheremail@example.com");

Reference

https://doc.rust-lang.org/stable/rust-by-example/index.html