Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.3

Type to search the documentation.

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

    Box and recursive data · 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.

    Section titled “1. Exercise · Represent the end of a link”

    Use prepend from the chapter to connect 5, 10, 20 with total 35. Also run the sum for an empty list.

    Completion: Totals are 35 and 0; sketch each next link and the final None.

    Hint: Create 20 first, then prepend 10 and 5.

    2. Build a program · Build a consuming length function

    Section titled “2. Build a program · Build a consuming length function”

    Write length using the same ownership transfer pattern as sum. Construct separate empty and three-node lists to pass to it.

    Completion: Results are 0 and 3; do not pass a list to length after consuming it with sum.

    Hint: Some contributes 1 plus the tail length; None contributes 0.

    The program renames a box, then consumes it through the old name. Fix the owner used without creating a duplicate box.

    Start with this deliberately broken program.

    class_move.xen
    fn main() {
    let original = box(42);
    let transferred = original;
    println(original.into_inner());
    }
    Terminal window
    xen check class_move.xen

    Diagnostic excerpt:

    class_move.stderr
    value moved here

    Completion: 42 prints once; identify the assignment transferring ownership.

    Hint: The new owner is transferred; into_inner also consumes the box.

    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.