kota's memex

A scalar type represents a single value. Rust has four primary scalar types: integers, floating-point numbers, Booleans, and characters.

integer types

Like other languages an integer is a number without any fractional component.

Length  Signed Unsigned
8-bit   i8     u8
16-bit  i16    u16
32-bit  i32    u32
64-bit  i64    u64
128-bit i128   u128
arch    isize  usize

In most normals cases the defaults i32 are reasonable. The primary situation in which you’d use isize or usize is when indexing some sort of collection.

overflow

When you’re compiling in debug mode, Rust includes checks for integer overflow that cause your program to panic at runtime if this behavior occurs.

For release mode, if overflow occurs, Rust performs two’s complement wrapping. In short, values greater than the maximum value the type can hold wrap around to the minimum of the values the type can hold. In the case of a u8 , the value 256 becomes 0, the value 257 becomes 1, and so on.

literals

Decimal 98_222
Hex     0xff
Octal   0o77
Binary  0b1111_0000
Byte    b'A'

to string

You can use rust's built-in string formatting or you can use this much faster crate: https://crates.io/crates/itoa

floating point

Rust's floating-point types are f32 and f64 , which are 32 bits and 64 bits in size, respectively. The default type is f64 because on modern CPUs, it’s roughly the same speed as f32 but is capable of more precision. All floating-point types are signed.

conversion

Casting from an f64 to an f32 will produce the closest possible f32.

let double: f64 = 42.;
dbg!(double as f32);

booleans

It's called bool in rust.

char

Rust's char type is the language's most primitive alphabetic type. A char is 4 bytes and represents a unicode scalar value. HOWEVER, unicode does not have a concept of "characters" and a char is not equivalent to a unicode code point.

fn main() {
  let c = 'z';
  let z: char = 'ℤ'; // with explicit type annotation
  let heart_eyed_cat = '😻';
}