Structs and enums · Coding Class
This topic is not available in the selected language or version. Showing the start of this section.
Apply the chapter in three steps: an exercise, a program project, and a debugging task. Implement first, then consult the hints and completion criteria. Use a separate file or working directory for each task.
1. Exercise · Classify states
Section titled “1. Exercise · Classify states”Declare enum Status { Pending, Done, Failed(String) } and write a function formatting each state.
Completion: Pending formats as pending, Done as done, and Failed(“disk”) as failed: disk.
Hint: Make every match branch return String.
2. Build a program · Build a task card
Section titled “2. Build a program · Build a task card”Define Task with String title and Bool done, plus a label method including the state. Print incomplete and complete cards titled read.
Completion: Output is todo: read, done: read. Make the variable mutable if changing its state.
Hint: Use the chapter’s &self method call form; there is no need to create explicit references.
3. Debug · Fix an incomplete match
Section titled “3. Debug · Fix an incomplete match”Check the code below and handle the missing state explicitly. Name both variants instead of using _.
Start with this deliberately broken program.
enum State { Ready, Waiting }fn label(state: State) -> String { return match state { State.Ready => "ready", };}fn main() { println(label(State.Waiting)); }xen check class_match.xenDiagnostic excerpt:
non-exhaustive match; missing WaitingCompletion: Ready returns ready, Waiting returns waiting, and checking succeeds.
Hint: Every enum variant must be covered.
Verify and record
Section titled “Verify and record”Use xen check file.xen followed by xen run file.xen for ordinary programs; use xen test file.xen for test-only files. Record the original error, its cause, and the corrected result, then rerun every input in the completion criteria.