Xen / examples v0.1.0-demo.2
Read a file
This topic is not available in the selected language or version. Showing the start of this section.
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.
printf 'one two\nthree four\n' > input.txtxen run examples/word_count.xen -- input.txtExpected output
Section titled “Expected output”4Complete source
Section titled “Complete source”// SPDX-License-Identifier: MIT OR Apache-2.0use 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)); }, };}Build an executable
Section titled “Build an executable”xen build examples/word_count.xen -o appRun the generated executable on a compatible Linux machine.