Values and primitive types
Constants that have been initialized have a value. Values exist in different types: 70 is an integer, 3.14 is a float, and Z and θ are the type of a character. Characters are Unicode values that take four bytes of memory each. Godzilla is a string of type &str (which is Unicode UTF8 by default), true and false are the type of Boolean values. Integers can be written in different formats:
- Hexadecimal format with 0x, like 0x46 for 70.
- Octal format with 0o, like 0o106 for 70.
- Binary format with 0b, like 0b1000110.
- Underscores can be used for readability, as in 1_000_000. Sometimes the compiler will urge you to indicate more explicitly the type of number with a suffix, for example (the number after u or i is the number of memory bits used, namely: 8, 16, 32, or 64).
- The 10usize denotes an unsigned integer of machine word size (usize), which can be any of the following types: u8, u16, u32, u64.
- The 10isize denotes a signed integer of machine word size (isize), which can be any of the following types: i8, i16, i32, i64
- In the cases above on a 64-bit operating system usize is in fact u64, and isize is equivalent to i64.
- The 3.14f32 denotes a 32-bit floating-point number.
- The 3.14f64 denotes a 64-bit floating-point number.
- The numeric types i32 and f64 are the defaults if no suffix is given, but in that case to differentiate between them you must end an f64 value with .0, like:
let e = 7.0;
In general, indicating a specific type is recommended.
Rust is like any other C-like language when it comes to the different operators and their precedence. However, notice that Rust does not have increment (++) or decrement (--) operators. To compare two values for equality use == , and != to test if they are different.
There is even the empty value () of zero size, which is the only value of the so called unit type (). It is used to indicate the return value when an expression or a function returns nothing (no value), as is the case for a function that only prints to the console. () is not the equivalent of a null value in other languages; () is no value, whereas null is a value.