Rust datatypes
From wikinotes
Documentation
primitives https://doc.rust-lang.org/std/#primitives
Literals
'a' // char "abc" // string 1234 // i32 3.14 // f32 true/false // bool1_000 // == 1000 1.000_000 // == 1.000000 0xff // hex Oo644 // octal 0b1111_0000 // binary b'A' // byte (u8)
Primitives
Text
str
https://doc.rust-lang.org/std/primitive.str.html
let name: &str = "vaderd"; // assign string let mut user = String::new(); // utf8 string // type casting 'string' to 'i8' let num: i8 = "123" .parse() .unwrap();Some Useful Methods
"abc".len() // 3 number of bytes used "abc".ends_with("bc") // true if ends withchar
chars refer to a single character, and it's literals use single-quotes.
chars use 4-bytes in memory; they can store multibyte characters.let foo: char = 'a';Numbers
implied type let var = 12;
assigned type let var: i8 = 12;
type suffix let var = 12i8;
Integers
- signed integers range is split in two, can be positive/negative
- unsigned integers are positive, and use all available bits
- use radix to calculate max size that can be accomodated with b bits
// signed integers, by bit-size i8 // -128..127 i16 // -32768..32767 i32 // -2147483648..2147483647 i64 // ... i128 isize // your CPU wordsize (ex. i32 or i64) // unsigned integers, by bit-size u8 // 0..255 u16 // 0..65535 u36 // 0..4294967295 u64 // ... u128 usize // your CPU wordsize (ex. u32 or u64)Floating Point
f32 f64Boolean
true false
Collections
tuples
- tuples can store mixed types
- tuples cannot be resized.
- tuples can contain other tuples
let var: (i8, char, u32) = (5, 'a', 300); var = (1, "two", 3.14) var.0 // item at index 1arrays
- arrays are homogenous
- arrays cannot be resized.
- arrays are stored contiguously in memory
// initialization let var: [i32; 4] = [1, 2, 3, 4]; // declare an array of 4x 32-bit integers let var: [i32; 4] = [100; 4]; // initialize all 4x ints as 100 // methods var[0] // 1 var.len() // 4 // slices let foo = &var[1..2]; // [2, 3] println!("{}", foo[0]); // 2slices
slices are a subsection of an array
let var: [i8; 4] = [1, 2, 3, 4]; let first_two = &var[0..1]; // [1, 2] println!("{}", foo[0]); // 2vectors
vectors are essentially resizable arrays.
- homogenous
- resizable
- stored in contiguous memory
https://doc.rust-lang.org/std/vec/index.html
let v: Vec<i32> = Vec::new(); let v = vec![1, 2, 3, 4, 5];structs
struct Point { x: u8, y: u8 } // struct struct Unit // unit struct struct Color(i8, i8, i8); // tuple-structstruct
struct Point { x: u8, y: u8 }; let p: Point = Point { x: 5, y: 10 };unit struct
// an immutable trait-like object that stores no value. struct TestRuntuple struct
struct Color(i8, i8, i8); let c: Color = Color(100, 150, 200);
Pointers
Pointers
Function Pointers
References
Other
Enums
enum TaskStatus { Blocked, Ready, Started, Finished, } TaskStatus::ReadyYou can also store complex information in a struct
enum Event { KeyPress(char), // like tuple-struct Click { x: i32, y: i32 }, // like c-structs Blue = 0x0000ff, // assign value } Event::KeyPress('j')Scoping with
use
use TaskStatus::*; let foo = Blocked; use TaskStatus::{Blocked, Ready}; let foo = Blocked; let bar = Started; // raises error, since not in scopeResult
Results are chainable enums with success/failure values.