Structs and Enums in Rust Cheat Sheet

Rust is a system programming language that is designed to be fast, safe, and concise. It is gaining popularity among developers because of its excellent safety features, which prevent memory-related errors. cheats for rust has many unique concepts, one of which is its strong type system. A type system in Rust ensures that variables are assigned only those values that are compatible with their types. In this post, we will discuss Rust’s primitive data types.
Rust has seven primitive data types, and each of them has one or more variants. These are:
- Booleans:
Boolean data types are used to represent true or false values. In Rust, we use the `bool` keyword to represent booleans. A boolean takes one byte of space in memory. The most common use for booleans is in decision-making statements such as `if else` statements.
- Characters:
In Rust, a character is a Unicode scalar value. Characters are used to represent letters, digits, and other symbols used in a programming language. Characters are enclosed in single quotes `’ ‘` and take four bytes of space in memory.
- Signed Integers:
Signed integers are used to represent both positive and negative values. Rust has four signed integer variants, `i8`, `i16`, `i32`, and `i64`. The number represents the number of bits allocated for storing the integer. An `i8` will take 1 byte and can hold values in the range of -128 to 127, while an `i32` will take 4 bytes and can hold values in the range of -2^31 to 2^31 – 1.
- Unsigned Integers:
Unsigned integers are used to represent positive values only. Rust has four unsigned integer variants: `u8`, `u16`, `u32`, and `u64`. The number represents the number of bits allocated for storing the integer. An `u8` will take 1 byte and can hold values in the range of 0 to 255, while a `u32` will take 4 bytes and can hold values in the range of 0 to 2^32 – 1.
- Floating-Point:
Floating-point types are used to represent numbers with fractional parts or exponential values. Rust has two variants of floating-point data types: `f32` and `f64`. `f32` uses 32 bits of memory and can hold 7 significant decimal digits, while `f64` uses 64 bits of memory and can hold 15 significant decimal digits.
- Arrays:
Arrays are a collection of values of the same type. In Rust, we use the `[type; size]` syntax to declare an array. For example, `let numbers: [i32; 5] = [1, 2, 3, 4, 5];` declares an array `numbers` of length `5` which can hold `i32` data type.
- Tuples:
Tuples are used to group values of different types. In Rust, we use parentheses `()` to create a tuple. For example, let `person_tuple: (&str, i32, bool) = (“John”, 30, true);` creates a tuple of three values, where the first value is a string (`&str`), the second value is an integer of `i32` data type, and the third value is a boolean.
In conclusion, Rust’s type system is robust and designed to prevent common memory-related issues that plague other programming languages. Understanding the seven primitive data types and their variants is essential when programming in Rust. Rust also has additional data types such as slices, Strings, and vectors, which build upon the seven primitive data types. Happy coding!