
Understand it
A local playground executes untrusted source on the user's own device, so a useful sandbox must bound more than filesystem access. A program can loop forever, allocate aggressively, print without end, or fill its writable directory. Each consumes a different resource. A wall-clock deadline alone may detect a slow run only after memory or output has already exhausted the host.
Define budgets at the layer that owns the resource. An interpreter may provide fuel or interruption for instruction execution; the host can cap captured output and writable bytes; the embedding can limit memory pages and concurrent runs. Cancellation should release the owned run and its resources, while the UI reports a quota stop distinctly from a successful program exit or an ordinary Rust diagnostic.
The compiler lab is a small model of output accounting. Starting from a known usage, accept the next chunk only if checked addition stays within an inclusive limit. Refuse overflow and an already-invalid starting state. An empty chunk at the exact limit is valid. This function does not enforce the actual runtime's limits; it teaches why checking after an unbounded allocation is too late.
Local compiler profile: standard library only. In a shared Playground, code and selection changes may be interactive while execution remains local and explicit. Another participant's Run should update shared output metadata rather than start your device's compiler. A production safety review must test those event boundaries separately from this arithmetic model, including disconnection, repeated messages, and a local cancel during remote edits.
Read the code
fn reserve(
used: usize,
next: usize,
limit: usize,
) -> Option<usize> {
let total = used.checked_add(next)?;
(total <= limit).then_some(total)
}
fn main() {
assert_eq!(reserve(8, 2, 10), Some(10));
assert_eq!(reserve(8, 3, 10), None);
}
Independent CPU, memory, output, and storage limits
Try it
Return updated usage only when checked addition succeeds and the inclusive limit is respected. Reject an invalid starting usage as well as overflow. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Which statement about a local execution timeout is correct?
Reveal the answer
- It makes all allocations safe before it fires
- It automatically limits printed output to one line
- It is one limit and does not replace separate resource quotas
CPU time, memory, output, storage, and concurrency need limits at their own boundaries.
Go deeper
Common misconception: A program is harmless to local resources because it never runs on the backend.
Local execution avoids backend computation but still consumes the device's resources.
- Measure: Identify the independently consumed resource.
- Reserve: Check an inclusive limit before accepting another chunk.
- Stop: Report quota termination and release the local run.
Transfer challenge: Design a test matrix distinguishing a Rust panic, compilation error, user cancel, CPU budget, output cap, and writable-storage cap.
Connections
- Linear Memory Boundaries: Checked offsets, lengths, and borrowed slices