Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.2

Type to search the documentation.

    한국어
    Xen / examples v0.1.0-demo.2

    Read a file

    Read a file

    Run from the repository or distribution root. Put xen on PATH, or substitute ./bin/xen.

    The first command creates or overwrites input.txt in the current directory. Exactly one path is required; a missing file terminates with an error.

    Terminal window
    printf 'one two\nthree four\n' > input.txt
    xen run examples/word_count.xen -- input.txt
    4
    word_count.xen
    // SPDX-License-Identifier: MIT OR Apache-2.0
    use std.env as env;
    use std.fs as fs;
    use std.io as io;
    use std.string as text;
    fn count_file(path: String) -> Result<Int, io.IoError> {
    let contents = fs.read(path)?;
    return Result.Ok(text.split_ascii_whitespace(contents).len());
    }
    fn main() {
    let arguments = env.args();
    if arguments.len() != 1 {
    panic("usage: word_count <path>");
    }
    match count_file(arguments.get(0)) {
    Result.Ok(count) => println(count),
    Result.Err(error) => { panic(io.describe_error(error)); },
    };
    }

    Download file

    Terminal window
    xen build examples/word_count.xen -o app

    Run the generated executable on a compatible Linux machine.