Rust has a list of data types that are said to be primitive. In this article, we will go over this list of primitive data types in Rust — which we will group into scalar and compound types — and the limitations of primitive data types in Rust.
We will cover:
- What are primitive data types?
- Review of the Rust programming language
- Primitive data types in Rust
- Limitations of Rust primitive types
This article is intended to benefit those who are just getting started in Rust and want to quickly understand and be able to use the primitive data types in Rust.
What are primitive data types?
Primitive data types, as the name implies, are basically just data types that come with a programming language. They are built-in and, when combined together, can form more complex data types, which are called non-primitive data types.
As we mentioned, Rust programming language comes with a list of built-in primitive data types that developers can use as building blocks for other data types.
Review of the Rust programming language
Rust describes itself as a systems programming language that runs blazingly fast, prevents all crashes, and eliminates data races. It is good in memory efficiency because it has no runtime or garbage collector. For this and many other reasons, Rust is a popular and well-loved programming language.
Right now, this programming language and its community are relatively young. This means there are ongoing developments to add, improve, and stabilize various features, methods, and trait implementations.
You can use Rust comfortably with a simple command line interface (CLI), WebAssembly (Wasm), Networking, and Embedded.
People mostly like to talk about if Rust is worth learning or has a steep learning curve. But ultimately, it is up to each individual to determine if it is worth learning for your personal needs or not.
Of course, there are most certainly things to note that set Rust apart:
- It is a well-designed language
- It pays attention to correctness and safety compared to other languages
- It has good concurrency and speed
- It has a very strong community
Rust was designed by Graydon Hoare and made its first appearance in 7 July 2010. As of this article’s publication, this programming language is currently on version 1.63.0, which was announced on 11 August 2022.
Primitive data types in Rust
Let’s look at the primitive data types Rust provides.
We want to first group them into scalar and compound data types. The difference between these two is that compound types contain multiple values in a type while scalar types contain just one value.
Scalar primitive types in Rust
There are five scalar primitive types you should be familiar with in Rust:
Let’s look at definitions and examples for each type.
bool
data type
The Boolean data type is said to be either true or false, like so:
let active = true; let inactive = false;
Boolean data types are mostly used to compare values or logic — for example, to check if a test score is A, B, or C.
char
data type
The character type is a 4-byte data type. It is used to store single characters, such as:
let first = 'a'; let second = 'b'; let symbol = '∞';
Character data types are used to store single characters, allowing the memory allocation in Rust to remain small.
integer
data type
There are various integer data types, which fall under two categories: signed (i) and unsigned (u). They include the following: i8
, i16
, i32
, i64
, isize
, u8
, u16
, u32
, u64
, usize
. Here are some examples:
let height = 172; //u8 let weight = 68; // u8 let size = 23; // u8 let data = -128 // i8
floating
data type
Floating data types are always either f32
or f64
, which can range widely from negative to positive numbers:
f32 ---> -3.8x10^8 to +3.8x10^8 f64 ---> -1.8x10^308 to +1.8x10^308
Floats are what we refer to as decimals. See some examples below:
let interest = 1.20; let returns = 2.80; let agency = 10.0;
unit
data type
In Rust, the unit data type uses the symbol ()
and it is mostly used as a mechanism to avoid using null
.
Any expression that returns nothing actually returns ()
in Rust. It is more like void
in C-like languages.
Another use case is like Response<(), String>
which means the response can either fail or be successful.
Compound primitive types in Rust
Below are four compound primitive data types in Rust that we will cover below:
As we did in the previous section, let’s look at definitions and examples for each type.
array
data type
An array is a data type that contains a group of elements. Its size is always fixed and of the same data type, like so:
let counts: [i32; 7] = [4, 2, 4, 8, 3, 2, 4, 8]; let grade: [i32; 4] = [20, 40, 34, 70];
In the examples above, the counts
array contains 7
elements of data type i32
(integers), while the grade
array contains 4
elements of data type i32
.
string
data type
There are two string
data types in Rust: String
(String Object) and &str
(String literal).
The String
object is not in the core language, but is provided in the standard library. It is also the most common string type because it is mutable. To create a String
:
String::new(); let name = String::new(); name.push_str = 'Victor Jonah'; println("{}", name);
The &str
data type in Rust is regarded as string slice and it is said to be immutable, which means they cannot be changed during the lifetime of the program. Take a look at the example below:
let name:&str = 'Victor Jonah'; let company:&str = 'LogRocket';
In the example above, during the lifetime of that program, name
will always be associated with the string Victor Jonah
, while company
will always be associated with the string LogRocket
.
slice
data type
Slices are similar to arrays, but there are a few differences.
While array sizes are fixed, slices are dynamic in size; the length is not known in compile time and the data is sliced into a new collection. See an example below:
let grades: [132:6] = [20, 10, 30, 40, 50, 10, 20]; let slice = &[20...4]; // 20, 10, 30, 40
Slices are also a pointer to the string object above where we can retrieve a certain character in the string value. We can also borrow elements in a slice to use somewhere else.
tuple
data type
In other languages like JavaScript, tuples are referred to as objects. They are fixed data types that contain different types of elements — unlike arrays, which can only contain the same type of elements.
let employee: (&str, i32, &str) = ('Victor Jonah', 25, 'Technical Writer');
In the example above, the tuple employee
contains three elements: a string (Victor Jonah
), an integer (25
), and another string (Technical Writer
).
Limitations of Rust and its primitive types
As a side note, it will be essential to discuss the limitations of the Rust programming language in general. Most people have said or claimed that Rust is very lovable — which is true — but there a few points to consider.
The first thing to note is that its learning curve is steep; Rust takes more time to learn as a language because it is a system programming language and has high-level programming concepts.
There is a lot to learn when it comes to Rust primitive data types and combining them together — like pattern matching, pointers, string literals, three types of arrays, and more. Nevertheless, it is worth your time.
From my observation, the steep learning curve mostly results from the lack of clarity in the Rust documentation in the earlier days of working with Rust.
This brings me to a second note: the Rust community may be less noticeable at the very beginning, but when you reach out, the community is welcoming, active, and helpful.
Another thing to note is that Rust is a static programming language, and it is very strict to the point that everything has to be stated before it is compiled. This is one of the main principles of Rust, which enforces that everything should be checked at compile time.
This can slow down development, but is also for a good cause because when most things are checked at compile time, it is less likely that the program will fail at runtime.
Conclusion
Rust primitive data types are built-in and their use cases are exactly what a typical programming language needs. These data types come in two forms: scalar and compound.
Knowing and understanding all the different primitive data types in Rust is very helpful in your Rust journey. I have made this article on the brief side for that purpose. Thank you for reading.
The post Understanding primitive data types in Rust appeared first on LogRocket Blog.
from LogRocket Blog https://ift.tt/GeklJiF
Gain $200 in a week
via Read more