-
Intro to Rust - Collections
Friday, March 6, 2020
Collections are data structures that contain multiple values. However, unlike arrays and tuples that are stored on the stack, these types are stored on the heap. Vectors Vectors are similar to arrays except they can grow dynamically. Like an array, it can only store a single data type. To a create a new vector, we write: let a: Vec<u32> = Vec::new(); The type annotation above is necessary as we’ve created an empty vector so Rust has nothing to infer from.…more
-
Intro to Rust - Structs
Tuesday, March 3, 2020
A struct is a data type that allows you to group multiple related values under a single type. To define a struct in Rust, we use the struct keyword: struct Point { x: i32, y: i32, } And we create an instance of the struct like so: let p = Point{ x: 3, y: 4 }; And to access a field on the struct we use dot notation: println!("x: {}, y: {}", p.…more
-
Intro to Rust - Ownership
Monday, March 2, 2020
Every programming language has to have some way to manage the memory it consumes while the program is running. In higher level languages like JavaScript this is managed with a garbage collector or with lower level languages like C, the memory is allocated and freed manually. Rust uses a different approach with a concept known as ownership. Stack and Heap The stack and heap are parts of memory that your program can utilize at runtime.…more
-
Intro to Rust - Variables
Wednesday, February 26, 2020
Variables are conceptually labeled boxes that we can store values. The box is the address in memory where the value is stored and the label is just a human readable alias for that address. All variable declarations in Rust start with the let keyword followed by the name we assign to variable. Here we are declaring a variable called z and assigning the value 12 to it: let z = 12; By default, all variables in Rust are immutable meaning you cannot re-assign a value to them.…more
-
Intro to Rust - Hello World
Tuesday, February 25, 2020
Installing and configuring Rust is a breeze with rustup which is an installer for Rust. You can download it by visiting: https://rustup.rs/ And following the onscreen instructions. You can confirm that the installation has worked by running: rustc --version The rustup installation also comes with another command line tool called cargo which is the Rust packager manager – akin to npm for Node. Rust packages are called crates and there is an online repository of crates visible here:…more