Master the fundamental concepts of rust for systems programming through this focused micro-challenge.
Ownership is why Rust ships in the Linux kernel, Windows, and Android Bluetooth without a GC. The borrow checker encodes rules that C leaves to discipline and sanitizers (see The Rust Book: What is Ownership?).
drop runsrustLoading…
String, Vec, Box) transfer heap ownershipi32, bool, and other trivial types) duplicate bits&T or one &mut T at a time (References and Borrowing)In C there is no borrow checker: "move" is a convention (hand off a pointer and clear the old one), "copy" is assignment of scalar values, and "borrow" is passing a pointer without transferring free responsibility.
Read a mode from stdin (move, copy, borrow, or all) and print the exact lines required by the tests. Prefer the Rust language selector for the idiomatic exercise; the C starter teaches the same ideas with pointers so both languages stay valid.
Build a mini borrow checker. It reads a tiny straight-line language that models Rust's ownership rules, reports every violation with the matching rustc error code and, if the program is clean, prints the order in which owned values are dropped.
One statement per line. Blank lines, a trailing ; and // comments are ignored.
cLoading…
Check the statements in order. A reference is live from its let to the last later statement that mentions its name (non-lexical lifetimes).
| Situation | Error |
|---|---|
| any statement names an undeclared or out-of-scope variable | E0425 cannot find value in this scope: |
use / copy / move / drop of a moved variable | E0382 use of moved value: |
&x, &mut x or mutate x on a moved variable | E0382 borrow of moved value: |
read or &x while a live &mut of x exists | E0502 cannot borrow as immutable because it is also borrowed as mutable: |
&mut x / mutate x on x without mut | E0596 cannot borrow as mutable, as it is not declared as mutable: |
mutate r where r is a & reference | E0596 cannot borrow data in a & reference as mutable: |
&mut x / mutate x while a live &mut of x exists | E0499 cannot borrow as mutable more than once at a time: |
&mut x / mutate x while a live & of x exists | E0502 cannot borrow as mutable because it is also borrowed as immutable: |
move or drop of x while any live reference to x exists | E0505 cannot move out because it is borrowed: |
Check in the order of the rows within each group: moved first, then mutability, then conflicting &mut, then conflicting &. Each statement reports at most one error. A let always declares its new name, even if it failed (a failed move leaves the source intact). Integers and & references are Copy, so copying or dropping them only reads.
Other errors, also prefixed with line N: : syntax error: TEXT for an unrecognised line, syntax error: unmatched }, syntax error: unclosed { (reported at the last line), and error: `NAME` is already declared (shadowing is not supported here) when a visible variable already has that name. Taking a reference to a reference is reported as error: borrowing a reference (`NAME`) is not supported here.
Each error prints as line N: error[CODE]: MESSAGE `NAME`, in line order. Then:
error: aborting due to K previous error(s).borrow check passed (K statements) (counting every non-blank statement, braces included), then one line per drop of an owned, unmoved String. drop x prints line N: drop x (explicit), a closing } drops its scope's Strings in reverse declaration order as line N: drop x (end of scope), and at the end of the program the remaining Strings drop in reverse order as end: drop x.Input:
cLoading…
Output:
cLoading…
&mut reference moves it; copying a & reference does not.Hidden tests cover borrows kept alive through a copied reference, &mut aliasing and moving a &mut, writes through & and non-mut owners, moves out of borrowed values, scopes and drop order, syntax errors and several errors in one program.
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