let x = true;
let y: bool = false;
// ⭐️ no TRUE, FALSE, 1, 0
let x = 'x';
let y = '😎';
// ⭐️ no "x", only single quotes
Because of Unicode support, char is not a single byte, but four.
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();
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();
The actual bit size depends on the computer architecture you are compiling your program for. By default, the sizes are equal to 32 bit on 32-bit platforms and 64 bit on 64-bit platforms.
🔎 Search more about cross-compiling and Supported Tiers of Rust programs.
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.
💡 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.
let a = [1, 2, 3]; // a[0] = 1, a[1] = 2, a[2] = 3
let mut b = [1, 2, 3];
let c: [i32; 0] = []; //[Type; NO of elements] -> [] /empty array
let d: [i32; 3] = [1, 2, 3];
let e = ["my value"; 3]; //["my value", "my value", "my value"];
println!("{:?}", a); //[1, 2, 3]
println!("{:#?}", a);
// [
// 1,
// 2,
// 3
// ]
⭐️ 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.
let a = (1, 1.5, true, 'a', "Hello, world!");
// a.0 = 1, a.1 = 1.5, a.2 = true, a.3 = 'a', a.4 = "Hello, world!"
let b: (i32, f64) = (1, 1.5);
let (c, d) = b; // c = 1, d = 1.5
let (e, _, _, _, f) = a; //e = 1, f = "Hello, world!", _ indicates not interested of that item
let g = (0,); //single-element tuple
let h = (b, (2, 4), 5); //((1, 1.5), (2, 4), 5)
println!("{:?}", a); //(1, 1.5, true, 'a', "Hello, world!")
⭐️ 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.
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 other data structure), Rust allows for creating a view/reference to access only that part of 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]
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.
🔎 A String is a heap-allocated string. This string is growable and is also guaranteed to be UTF-8. They are commonly created by converting from a string slice using the to_string() or String::from() methods. ex:
“Hello”.to_string();
String::from("Hello");
💡 In general, you should use String when you need ownership, and &str
when you just need to borrow a string.
functions
As we discussed in the functions section,b
is a function pointer toplus_one()
in the below code.
fn plus_one(a: i32) -> i32 {
a + 1
}
let b: fn(i32) -> i32 = plus_one;
let c = b(5); //6