Menu
CoddyTech
Course

Rust for Lua Developers

Build on Lua tables, closures, protected calls and metatables to learn Rust ownership, borrowing, enums and traits. Complete 48 multi-file tasks, build a parcel depot and port two Lua programs.

Lua and Rust side by side

The same program in Lua and in Rust, from the lessons of this course.

  1. Output and Comments

    Lua executes a loaded chunk, while a Rust executable starts at fn main. Lua line comments start with two hyphens and long comments use bracket delimiters; Rust uses slash comments. A hash inside quoted text remains data in both examples. Lua io.write performs…

    Lua
    1-- This line is ignored.2io.write("level:")3print(string.format("%d # literal", 2 + 3))
    Rust
    1fn main() {2    // This line is ignored.3    print!("level:");4    println!("{} # literal", 2 + 3);5}
  2. Boolean Conditions

    Lua treats only false and nil as falsey. An empty table, zero, and an empty string are all truthy, so items or fallback selects the table in this example. Lua and and or short-circuit and return an operand rather than always producing a boolean. Rust…

    Lua
    1local items = {}2local chosen = items or "fallback"3print(chosen == items)4print(not not items)
    Rust
    1fn main() {2    let items: Vec<i32> = Vec::new();3    let present = !items.is_empty();4    println!("{}", present);5    println!("{}", present && items[0] == 9);6}
  3. Functions and Returns

    Lua function parameters accept values dynamically, and an explicit return produces results. Falling off the end returns no values, which becomes nil when one result is expected. Rust signatures specify parameter and return types, and a final expression…

    Lua
    1local function differs(left, right) return left ~= right end2print(differs("a", "b"))3print(differs("a", "a"))
    Rust
    1fn differs(left: char, right: char) -> bool { left != right }2fn main() {3    println!("{}", differs('a', 'b'));4    println!("{}", differs('a', 'a'));5}
  4. Ownership and Moves

    Lua table assignment creates another reference to the same table. Swapping through second therefore changes the values observed through first, and rawequal confirms table identity. Rust Vec instead has an owner; assigning it to another binding moves ownership…

    Lua
    1local first = {6, 8}2local second = first3second[1], second[2] = second[2], second[1]4print(first[1])5print(rawequal(first, second))
    Rust
    1fn main() {2    let first = vec![6, 8];3    let mut second = first;4    second.reverse();5    let mut independent = second.clone();6    independent.clear();7    println!("{}", second[0]);8    println!("{}", independent.len());9}
  5. Strings and UTF-8

    Lua strings store arbitrary bytes and are not inherently guaranteed to contain valid UTF-8. The utf8 library decodes this valid sample to obtain its first Unicode code point, while string.byte returns the first encoded byte. Rust str and String always contain…

    Lua
    1local word = "éx"2print(utf8.char(utf8.codepoint(word, 1)))3print(string.byte(word, 1))
    Rust
    1fn main() {2    let word = "éx";3    println!("{}", word.chars().next().unwrap());4    println!("{}", word.get(0..1).is_none());5}
  6. Option and Matching

    Ordinary Lua lookup returns nil for an absent key, while zero remains a stored value and is truthy. Rust Option<T> makes presence part of the type with Some and None. Some(0) is distinct from absence. The examples inspect a present first element and a missing…

    Lua
    1local values = {0}2local first = values[1]3local missing = values[3]4print(first == nil)5print(missing == nil)
    Rust
    1fn main() {2    let values = vec![0];3    println!("{}", values.get(0).is_none());4    println!("{}", values.get(2).is_none());5}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for Lua developers

Learn Rust from another language

Frequently asked questions

Who is Rust for Lua Developers for?

For people who already write Lua and want to learn Rust. Each lesson starts from Lua you know and shows how Rust does the same thing, so you skip the beginner basics.

Do I need to know Rust already?

No. The course starts with your first Rust program. You need Lua basics: variables, conditions, loops, functions and collections.

How many lessons does Rust for Lua Developers have?

Rust for Lua Developers contains 48 interactive lessons you can complete at your own pace.

What does Rust for Lua Developers cover?

The course is organized into chapters covering: First Rust Programs, Decisions and Loops, Functions and Sequences, Ownership and Borrowing, and more.

Is Rust for Lua Developers free?

Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.

Coddy programming languages illustration

Start Rust for Lua Developers for free

Get started