Menu
CoddyTech

Ownership and Moves

Lesson 16 of 48 in Coddy's Rust for Luau Developers course.

Luau assigning a typed table creates another reference to the same container; its type annotation does not make the assignment a copy or move. Swapping through second changes what first observes, and rawequal confirms identity. Rust Vec instead has an owner, and assignment transfers ownership unless an explicit clone is requested. The old binding becomes unavailable, though the allocation remains alive under the new owner. The Rust example clones its reversed vector and clears only that independent copy. Luau table.clone is shallow and may still share nested objects. Ordinary Rust integers are Copy, but Vec is not. Distinguish aliasing, moving ownership, and duplicating contents.

Complete standalone Luau program:

local first: {number} = {6, 8}
local second = first
second[1], second[2] = second[2], second[1]
print(first[1])
print(rawequal(first, second))

Luau output:

8
true

Complete standalone Rust program:

fn main() {
    let first = vec![6, 8];
    let mut second = first;
    second.reverse();
    let mut independent = second.clone();
    independent.clear();
    println!("{}", second[0]);
    println!("{}", independent.len());
}

Rust output:

8
0
challenge icon

Challenge

Easy

Implement pub fn dispatch(mut label: String) -> (String, usize) in parcel.rs. It takes ownership of the label, records its original UTF-8 byte length, appends :sent, and returns the resulting owned string and original byte length. Empty labels are valid. Implement solve(input: &str) -> String: treat the entire input, including any spaces, as the label and return length|dispatched-label. Inputs contain no newlines. Use the API; the locked driver also calls it directly.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, parcel.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod parcel;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
let owned = String::from("hold-é");
let backup = owned.clone();
assert_eq!(parcel::dispatch(owned), (String::from("hold-é:sent"), 7));
assert_eq!(backup, "hold-é");
assert_eq!(parcel::dispatch(String::new()), (String::from(":sent"), 0));
assert_eq!(parcel::dispatch(String::from(":sent")), (String::from(":sent:sent"), 5));

    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Rust for Luau Developers

Practice on your own: Online Rust compiler