Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.2

Type to search the documentation.

    한국어
    Xen / coding-class v0.1.0-demo.2

    Structs and enums · Coding Class

    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.

    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.

    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.

    Check the code below and handle the missing state explicitly. Name both variants instead of using _.

    Start with this deliberately broken program.

    class_match.xen
    enum State { Ready, Waiting }
    fn label(state: State) -> String {
    return match state { State.Ready => "ready", };
    }
    fn main() { println(label(State.Waiting)); }
    Terminal window
    xen check class_match.xen

    Diagnostic excerpt:

    class_match.stderr
    non-exhaustive match; missing Waiting

    Completion: Ready returns ready, Waiting returns waiting, and checking succeeds.

    Hint: Every enum variant must be covered.

    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.