Back
Close

Getting Started With Rust

lil_firelord
92.3K views

Primitive Data Types in Rust

  • bool : The boolean type.
  • char : A character type.
  • i8 : The 8-bit signed integer type.
  • i16 : The 16-bit signed integer type.
  • i32 : The 32-bit signed integer type.
  • i64 : The 64-bit signed integer type.
  • isize : The pointer-sized signed integer type.
  • u8 : The 8-bit unsigned integer type.
  • u16 : The 16-bit unsigned integer type.
  • u32 : The 32-bit unsigned integer type.
  • u64 : The 64-bit unsigned integer type.
  • usize : The pointer-sized unsigned integer type.
  • f32 : The 32-bit floating point type.
  • f64 : The 64-bit floating point type.
  • array : A fixed-size array, denoted [T; N], for the element type, T, and the non-negative compile-time constant size, N.
  • slice : A dynamically-sized view into a contiguous sequence, [T].
  • str : String slices.
  • tuple : A finite heterogeneous sequence, (T, U, ..).

bool

A standard boolean. Can be either true or false

let t = true;
let f = false;

char

A 4 byte character.

let a = 'a';
let b = 'b';
let keyboard = '⌨';

integer types

These types include i8, i16, i32, i64, isize, u8, u16, u32, u64, usize. The letter denotes whether it is signed (i) or unsigned (u), and the number denotes the size of the integer. So the type i8 is an 8 bit, integer and a u64 is an unsigned, 64 bit integer. isize and usize are dependent upon the architecture of the computer.

let x = 5;
let life = 42;
let jenny = 8675309;

floating point types

These types include f32 and f64. A floating point number is what we typically refer to as a decimal.

let pi = 3.14;
let e = 2.718;

array

An array is fixed-size, collection of same-type elements.

It is declared like:

let name: [type; size] = [elem1, elem2, elem3, elem4];
let array: [i32; 5] = [0, 1, 2, 3, 4];

println!("The first element of the array is: {}", array[0]);

let mut counter = 0;
for x in array.iter(){
    println!("The element at index {} is {}", counter, x);
    counter += 1;
}

slice

A slice is a dynamically sized, "slice" into a collection of elements.

For instance if we wanted to take the first 3 elements of our array it would look like this:

let array: [i32; 5] = [0, 1, 2, 3, 4];

let slice = &array[0..3]; // This will select the elements at index 0, 1, and 2. The first number is inclusive and second number is exclusive.

for x in slice {
	println!("x is {}", x):
}

If the & operator is new to you, it is a "reference". I'm not going to cover it in depth this course as it would get a bit beyond the basic Getting Started and more into how memory is managed.

Simply put it references the actual memory of what an array "points" to. For instance, array is just a name we make to represent the memory block of our 5 integers. When we say &array[0..3] we're telling rust that we don't want to look at our variable "array", but the actual information that array stands for, in this case, the first, second, and third element of the array, (ranges are exclusive in the upper bounds which we will see more of in the Control Flow section)

str

A str is a "string slice", and is the most primitive string type.

let str = "Hello! I'm a str";

Now something that trips some people when getting started with rust is printing variables. In order to print this we would need to write:

println!("The value of our variable is: {}", str);

This is a formatted print. Each set of {} signals that the variables following this string literal will be put into the string. This will print "The value of our variable is: Hello I'm a str". If we were to simply write println!(str), we would get a compile error due to type mismatch. The best short and simple explanation of this I could find was on a stack overflow post:

'"foo" is a string literal, 8 is a numeric literal. let s = "foo" is a statement that assigns the value of a string literal to an identifier (variable). println!(s) is a statement that provides an identifier to the macro.' [-Shepmaster](https://stackoverflow.com/users/155423/shepmaster)

(Stack Overflow Question)[https://stackoverflow.com/questions/27734708/println-error-format-argument-must-be-a-string-literal/27734760]

tuple

Tuples are finite, heterogeneous, sequences. Let's unpack that quickly. First of all they are finite; this is fairly self-explanatory. They have a size, a fixed number of elements. They are heterogeneous. They can contain multiple different types. This is in contrast to an array, which can only contain elements of the same type. And lastly they are sequences, meaning they have an order, and most importanly they can be accessed by index (although in a different manner than arrays).

let tuple = ("hello", 42, "world", [3,6,9]);

println!("First element is {}", tuple.0);
println!("Second element is {}", tuple.1);
println!("Third element is {}", tuple.2);
let mut counter = 0;
for x in &tuple.3 {
    println!("Element {} of the fourth element is {}", counter, x);
    counter += 1;
}

While a mediocre example, you can see that a tuple can indeed hold a number of different types. They are referenced by dot.notation followed by the element number.

Playground

Data Types

Next Steps?

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io