Data types

In this article we will Rust data types and how they behave

Data types subsets

Rust is statically typed language. This means that Rust must know the type of all variables at compile time. It’s not always necessary to explictly mark the data type.

Rust compiler can normally infer which type we want to use, but it’s a good practice to do it.

Rust is composed from two subsets of data types:

  • Scalar
  • Compound

Scalar

Scalar subset could be simplified as single values data types. Scalar data types are the following:

  • Integer
  • Float
  • Boolean
  • Char

Integer

An integer is a number without fractional component. Inside integer there are two types:

  • Signed: They are always positive.
  • Unsigned: They can be positive or negative.

You can differenciate them in a very simple way. If you have to write this number on paper, do you need to write the sign?

  • If it’s yes, well it’s a signed integer.

  • If not, then it’s a unsigned integer.

1
2
3
4
fn main() {
	let x: i32 = -5; // Needs a sign, then signed
	let y: u64 = 5;  // It doesn't need a sign, then unsigned
}
Types of Integer
LengthSignedUnsigned
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
archisizeusize

Floating-Point

Floating point are numbers with decimal points.

1
2
3
fn main() {
	let x: f32 = 3.14;
}
Types of Floating-Point
LengthFloating type
32-bitf32
64-bitf64

Boolean

Boolean it’s a data type that can be either true or false.

1
2
3
fn main() {
	let check: bool = false;
}

Char

Char, which is different from strings, is the language’s most primitive alphabetic type and it’s specified with simple quotes.

1
2
3
4
fn main() {
	let char = 's';
	let emoji = '🐧';
}

It can also represent Chinese, Japanese, Korean, emoji and zero-width spaces.


Compound

Compound data types are those that can group multiple data types. Rust, with no crates, have two types:

  • Tuple
  • Arrays

Tuple

A tuple, also known as vector, is a general way to group values with different data types.

1
2
3
4
5
6
7
8
fn main() {
   let tup: (i32, u64, f32) = (-500, 4, 8.3);

   // Ways to access to tuple values
   let first  = tup.0;
   let second = tup.1;
   let (first, second, third) = tup; 
}

Arrays

The difference between arrays and tuples is that, in arrays, data types must be the same.

1
2
3
4
5
6
7
fn main () {
	let array: i64 = [1, 2, 3, 4];

	// Access array values
	let first  = array[0];
	let second = array[1];
}
Theme Stack designed by Jimmy