Primitive Types
Last updated
Last updated
Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data.
References:
A scalar type represents a single value. Rust has four primary scalar types:
Integer is a number without a fractional component.
Floating points: Rust has two primitive types for floating-point numbers (f32
, f64
), which are numbers with decimal points.
Character: char
We specify char literals with single quotes '
.
Four bytes in size and represents a Unicode Scalar Value, which means it can represent a lot more than just ASCII.
Accented letters; Chinese, Japanese, and Korean characters; emoji; and zero-width spaces are all valid char values in Rust.
Boolean type bool
in Rust has two possible values: true and false. Booleans are one byte in size.
Tuple:
A tuple is a general way of grouping together a number of values with a variety of types into one compound type.
Tuples have a fixed length: once declared, they cannot grow or shrink in size.
Array:
Every element of an array must have the same type.
Unlike arrays in some other languages, arrays in Rust have a fixed length.
Slice:
Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection.
A slice is a kind of reference, so it does not have ownership.
We create slices using a range within brackets by specifying [starting_index..ending_index]
, where starting_index
is the first position in the slice and ending_index
is one more than the last position in the slice.
In this exercise we only need to define variable is_morning
that should be the opposite off is_evening
.
We do it by using let is_evening = !is_morning;
In this exercise we can try what value char
can represent.
Rust’s char type is four bytes in size and represents a Unicode Scalar Value, which means it can represent a lot more than just ASCII.
Accented letters; Chinese, Japanese, and Korean characters; emoji; and zero-width spaces are all valid char values in Rust.
In this exercise we need to initialize array with at least 100
in length.
Array in Rust have multiple ways of initialization.
Init all the value inside the square bracket []
.
Define the type and length [i32; 5]
.
Initialize an array to contain the same value for each element by specifying the initial value, followed by a semicolon, and then the length of the array in square brackets [1; 100]
.
So we just need to use the last option to initialize array with same value with length 100
or more.
Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection.
A slice is a kind of reference, so it does not have ownership.
We create slices using a range within brackets by specifying [starting_index..ending_index]
, where starting_index
is the first position in the slice and ending_index
is one more than the last position in the slice.
So in this exercise we only need to get value index 1
until 4
from array a
.
In this exercise we need to extract the tuple variable.
To get the individual values out of a tuple, we can use pattern matching to destructure a tuple value, like this:
We can also access a tuple element directly by using a period (.
) followed by the index of the value we want to access.
In this exercise we need to get second value from the tuple which is index 1
hence let second = numbers.1;
.