Search:

Rust

About

Rust is a statically typed, compiled, multi-paradigm programming language developed and maintained by the Rust foundation with the Rust community.

Data Types

str

The str type, commonly referred to as a string slice, is an immutable reference to UTF-8 characters and does not have ownership.

String

The String type is an (optionally mutable) sequence of UTF-8 characters and does have ownership. If a type implements Into<String>, you can convert it to a String with the to_string() function.

Path

Similar to a str, but for file paths.

PathBuf

Similar to a String, but for file paths.

usize

An unsigned integer whose size is determined the amount of bytes it takes to reference a location in memory.

u8

An unsigned 8 bit integer.

u16

An unsigned 16 bit integer.

u32

An unsigned 32 bit integer.

u64

An unsigned 64 bit integer.

i8

A signed 8 bit integer.

i16

A signed 16 bit integer.

i32

A signed 32 bit integer.

i64

A signed 64 bit integer.

Syntax

Variables

Variables are defined with the let keyword:

let foo = "Hello from Rust";

They can also, optionally, have a type annotation:

let foo: String = "Hello from Rust".to_string();

Functions

Functions are defined with the fn keyword. All binary programs start at the main function:

fn main() {
    /* ... */
}

Functions can, optionally, have a return type. If a function has a return type, is must return a value of that type. To return a value, simply create/call that value without a semicolon at the end of the line (you can also return a value early with the return keyword):

fn foo() -> String {
    String::new()
}

Closures

Closures, sometimes called lambdas in other languages, work a bit differently in Rust due to it's memory management system. They are defined by a pair of bar characters (|), then a pair of curly braces with the closure's content. To move an outside value into a closure, you can use the move keyword.

let foo = || {
    println!("This is a closure!");
}

let bar = "This is another closure!";
_ = move || {
    println!("{}", bar); // The `bar` value is now moved into the closure, and cannot be accessed after it
}

Generics

Generics allow you use a broader array of types for things like function parameters. For example, you could have a function that accepts any type that implements Into<String>:

fn foo<S: Impl<Into<String>>>(bar: S) {
    let bar = bar.to_string();
    println!("bar is: {bar}");
}

Enums

Enums (AKA Enumerations) are a way to create a simple custom data type. For example if you wanted to keep track of the current programming language that a code editor is using, you could make a Languages enum like this:

enum Languages {
    Rust,
    Python,
    Lua,
    Zig,
}

Structs

Structs allow you to create more complex types and state instantiation. You could create the state of a text editor like this:

struct EditorState {
    current-line: usize,
    current-row: usize,
    idx: usize,
    buffer: String,
}

Traits

Traits allow you to create shared behaviour for types. One of the build-in traits is Default, which is super useful for struct instantiation. Let's use the EditorState struct as an example.

struct EditorState {
    current-line: usize,
    current-row: usize,
    idx: usize,
    buffer: String,
}

impl Default for EditorState {
    fn default() -> Self {
        Self {
            current-line: 0,
            current-row: 0,
            idx: 0,
            buffer: String::new(),
        }
    }
}

Now, whenever we need a new instance of EditorState, we can just call the default function.

let editor_state = EditorState::default();

Control Flow

If Statements

If statements are quite simple in Rust compared to some similar languages. Parentheses are not required for them.

let foo = true;
let bar = true;

if foo && bar {
    println!("Both values are true!");
}

Match

Pattern matching is an essential part of Rust's control flow. It is akin to switch-case statements in other languages. The _ case is the default/fallback.

let lang = "rust";

match lang {
    "rust" => pritln!("The language is Rust!"),
    "python" => pritln!("The language is Python!"),
    "lua" => pritln!("The language is Lua!"),
    "zig" => pritln!("The language is Zig!"),
    _ => println!("Unable to detect language.")
}