Master the fundamental concepts of rust for systems programming through this focused micro-challenge.
A node wants to point at the next node, but only one owner may exist. That tension is the subject of "Too Many Linked Lists": std::collections::LinkedList uses unsafe internally because safe singly-linked lists fight the type system.
rustLoading…
&mut self because you mutate head pointersOption<Box<Node>> ownership instead of raw linksFor this exercise, you will implement push, pop, and iterate on a safe singly-linked list. This task asks you to read compiler errors as design feedback, because choosing Vec over LinkedList in production is often a cache decision, but building the list teaches ownership better than any slide deck.
Keep the relevant man page, ABI doc, or Rust reference chapter open while you work. When your output disagrees with the reference implementation on the same machine, the mismatch is usually an alignment rule, an off-by-one terminator, or a register slot you misread in GDB.
Build a singly linked stack in safe Rust: struct Node { v: i32, next: Option<Box<Node>> }, then drive it from stdin commands. Every operation is a lesson in ownership: pop moves the head out with Option::take, peek returns a shared borrow, add goes through peek_mut, reverse relinks boxes without cloning, and remove walks a &mut Option<Box<Node>> cursor.
One command per line (blank lines are ignored). X is a 32-bit signed integer.
cLoading…
| Command | Output |
|---|---|
pop, peek | Some(V), or None on an empty list |
add X | the new front value as Some(V), None on an empty list, or overflow (value unchanged) if the result does not fit in an i32 |
len | len=N |
remove X | removed X or X not found |
print | [3, 2, 1] (Debug style, [] when empty) |
sum | sum=S |
A line with an unknown command or the wrong number of arguments prints unknown command: LINE. An argument that is not a valid i32 prints bad number: TOKEN.
Input:
cLoading…
Output:
cLoading…
unsafe, no Rc/RefCell, and no clone() of nodes. Ownership moves through Option<Box<Node>>.reverse and remove must relink the existing boxes in place.Hidden tests cover operations on an empty list, removing from the front, middle and end, removing a missing value, i32 overflow in add, i64 sums, and malformed commands.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works