title | slug |
---|---|
Primitive Data Types |
primitive-data-types |
true or false
let x = true;
let y: bool = false;
// ⭐️ no TRUE, FALSE, 1, 0
A single Unicode scalar value
let x = 'x';
let y: char = '😎';
// ⭐️ no "x", only single quotes
Because of Unicode support, char is not a single byte, but four(32 bits).
8, 16, 32, 64 and 128 bit fixed sized signed(+/-) integer types
DATA TYPE | MIN | MAX |
---|---|---|
i8 | -128 | 127 |
i16 | -32768 | 32767 |
i32 | -2147483648 | 2147483647 |
i64 | -9223372036854775808 | 9223372036854775807 |
i128 | -170141183460469231731687303715884105728 | 170141183460469231731687303715884105727 |
💡 The min and max values are based on the following equation; from -(2ⁿ⁻¹) to 2ⁿ⁻¹-1. You can use min_value()
and max_value()
functions to find min and max of each integer type. ex.i8::min_value();
let x = 10; // ⭐️ The default integer type in Rust is i32
let y: i8 = -128;
8, 16, 32, 64 and 128 bit fixed sized unsigned(0/+) integer types
DATA TYPE | MIN | MAX |
---|---|---|
u8 | 0 | 255 |
u16 | 0 | 65535 |
u32 | 0 | 4294967295 |
u64 | 0 | 18446744073709551615 |
u128 | 0 | 340282366920938463463374607431768211455 |
💡 The min and max values are based on the following equation; from 0 to 2ⁿ-1. Same way you can use min_value()
and max_value()
functions to find min and max of each integer type. ex.u8::max_value();
Pointer sized signed and unsigned integer types
The actual bit size depends on the computer architecture you are compiling your program for. By default, the sizes are equal to 32 bits on 32-bit platforms and 64 bits on 64-bit platforms.
🔎 Search more about cross-compiling and supported tiers of Rust programs.
32 and 64 bit sized floating point numbers(numbers with decimal points)
Rust follows IEEE Standard for Binary Floating-Point Arithmetic. The f32
type is similar to float(Single precision) in other languages, while f64
is similar to double(Double precision) in other languages.
let x = 1.5; // ⭐️ The default float type in Rust is f64
let y: f64 = 2.0;
💡 Should avoid using f32
, unless you need to reduce memory consumption badly or if you are doing low-level optimization, when targeted hardware does not support for double-precision or when single-precision is faster than double-precision on it.
Fixed size list of elements of same data type
let a = [1, 2, 3];
let a: [i32; 3] = [1, 2, 3]; // [Type; NO of elements]
let b: [i32; 0] = []; // An empty array
let mut c: [i32; 3] = [1, 2, 3];
c[0] = 2;
c[1] = 4;
c[2] = 6;
println!("{:?}", c); // [2, 4, 6]
println!("{:#?}", c);
// [
// 2,
// 4,
// 6,
// ]
let d = [0; 5]; // [0, 0, 0, 0, 0]
let e = ["x"; 5]; // ["x", "x", "x", "x", "x"]
⭐️ Arrays are immutable by default and even with mut
, its element count cannot be changed.
🔎 If you are looking for a dynamic/ growable array, you can use vectors. Vectors can contain any type of elements but all elements must be in the same data type.
Fixed size ordered list of elements of different(or same) data types
let a = (1, 1.5, true, 'a');
let a: (i32, f64, bool, char) = (1, 1.5, true, 'a');
let mut b = (1, 1.5);
b.0 = 2;
b.1 = 3.0;
println!("{:?}", b); // (2, 3.0)
println!("{:#?}", b);
// (
// 2,
// 3.0,
// )
let (c, d) = b; // c = 2, d = 3.0
let (e, _, _, f) = a; // e = 1, f = 'a'
let g = (0,); // single-element tuple
let h = (b, (2, 4), 5); // ((2, 3.0), (2, 4), 5)
⭐️ Tuples are also immutable by default and even with mut
, its element count cannot be changed. Also, if you want to change an element’s value, the new value should have the same data type of previous value.
Dynamically-sized reference to another data structure
Imagine you want to get/ pass a part of an array or any other data structure. Instead of copying it to another array (or same data structure), Rust allows for creating a view/ reference to access only that part of the data. This view/ reference can be mutable or immutable.
let a: [i32; 4] = [1, 2, 3, 4]; // Parent Array
let b: &[i32] = &a; // Slicing whole array
let c = &a[0..4]; // From 0th position to 4th(excluding)
let d = &a[..]; // Slicing whole array
let e = &a[1..3]; // [2, 3]
let f = &a[1..]; // [2, 3, 4]
let g = &a[..3]; // [1, 2, 3]
Unsized UTF-8 sequence of Unicode string slices
let a = "Hello, world."; // a: &'static str
let b: &str = "こんにちは, 世界!";
⭐️ It's an immutable/ statically allocated slice holding an unknown sized sequence of UTF-8 code points stored in somewhere in memory. &str
is used to borrow and assign the whole array to the given variable binding.
As we discussed in the functions section as well, p1
is a function pointer to plus_one()
in the following code.
fn main() {
let p1: fn(i32) -> i32 = plus_one;
let x = p1(5); // 6
}
fn plus_one(a: i32) -> i32 {
a + 1
}
- In Rust, the default integer type is
i32
and the default float type isf64
.
let i = 10; // Equals to `let i: i32 = 10;`
let f = 3.14; // Equals to `let f: f64 = 3.14;`
- Other than adding the type annotations to the variables, for numeric types, we can append the data type directly to the value as the suffix. Also, to improve the readability of long numbers, we can use
_
as a divider.
let a = 5i8; // Equals to `let a: i8 = 5;`
let b = 100_000_000; // Equals to `let b = 100000000;`
// 💡 The placements of _s are not strict. ex. 10000_0000 is also valid.
let pi = 3.141_592_653_59f64; // Equals to `let pi: f64 = 3.14159265359`
const PI: f64 = 3.141_592_653_59; // In the constants and statics, the data type must be annotated in the beginning.
-
There are several string types in Rust. The
String
type is a heap-allocated string. This string is growable and is also guaranteed to be UTF-8. In general, you should useString
when you needownership
, and&str
when you just need to borrow a string. -
A
String
type can be generated from a&str
type, via theto_string()
orString::from()
methods. Withas_str()
method, aString
type can be converted to a&str
type.
let s: &str = "Hello"; // &str
let s = s.to_string(); // String
let s = String::from(s); // String
let s = s.as_str(); // &str