Menu
CoddyTech

Port Rust Room Objects

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

challenge icon

Challenge

Medium

Port the Rust reference. First input line is nonnegative room capacity; remaining lines are signed integer reservation requests. Accept only strictly positive requests that fit. Failed requests leave used seats unchanged. Return remaining:accepted. Implement Model.new(capacity) and Model:reserve(seats) in model.lua; reserve returns a boolean. Use a fresh object per run.

Edit app.lua, model.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.

Public API checks: Model.new(capacity) exposes capacity and used, initially zero. reserve returns a boolean and updates used only for a positive request that fits. Rooms are independent. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.

Rust 2021 reference

use std::io::{self, Read};
struct Room { capacity: i64, used: i64 }
impl Room {
    fn new(capacity: i64) -> Self { Self { capacity, used: 0 } }
    fn reserve(&mut self, seats: i64) -> bool {
        if seats <= 0 || seats > self.capacity - self.used { return false; }
        self.used += seats;
        true
    }
    fn remaining(&self) -> i64 { self.capacity - self.used }
}
fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    let mut values = input.split_whitespace().map(|s| s.parse::<i64>().unwrap());
    let mut room = Room::new(values.next().unwrap());
    let mut accepted = 0;
    for seats in values { if room.reserve(seats) { accepted += 1; } }
    println!("{}:{}", room.remaining(), accepted);
}

The Rust struct keeps its fields private outside its module and exposes a shared-borrow observer. The Lua contract exposes capacity and used for the driver checks, while reservation behavior remains the same. Allocate each room independently; a failed request must leave its state unchanged. Do not use Lua logical operators as a boolean-to-integer conversion.

Try it yourself

-- Public API checks
do
local Model=require("model")
local a,b=Model.new(5),Model.new(2)
assert(a:reserve(6)==false and a.used==0,"oversized reservation must not mutate")
assert(a:reserve(0)==false and a:reserve(-1)==false and a.used==0,"nonpositive requests are rejected")
assert(a:reserve(2)==true and a.used==2,"accepted seats update used")
assert(a:reserve(3)==true and a.used==5 and a.capacity==5,"exact fit succeeds without changing capacity")
assert(b.used==0 and b.capacity==2,"rooms must be independent")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))

All lessons in Lua for Rust Developers

Practice on your own: Online Lua compiler