Build a word counter
This topic is not available in the selected language or version. Showing the start of this section.
Build a program that accepts a file path and prints the file’s word count. This combines variables, functions, strings, and error handling.
Prepare the input
Section titled “Prepare the input”Create a test file in your working directory:
printf 'one two\nthree four\n' > input.txtThis creates or overwrites input.txt in that directory. Save the following code as word_count.xen:
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)); }, };}$ xen run word_count.xen -- input.txt4Read the program
Section titled “Read the program”env.args() returns only arguments passed to the program, excluding the executable name. input.txt after -- becomes the first argument. Quote a path in your shell if it contains spaces.
count_file reads the whole file using fs.read, propagating a failure with ?. On success, it counts the items separated by ASCII whitespace. main prints that count or describes the read error and terminates.
Here a word means a sequence of bytes separated by ASCII whitespace. The program does not remove punctuation or analyze language-specific words. It reads the whole file at once, so it is unsuitable for very large files.
Try failures too
Section titled “Try failures too”Pass a path that does not exist:
xen run word_count.xen -- missing.txtYou will see a file-opening error and a failure exit status. Omitting the path after -- produces usage: word_count <path>. The precise file error number depends on your environment.
Make a standalone executable
Section titled “Make a standalone executable”xen build word_count.xen -o word-count./word-count input.txtThe result is again 4. Direct execution does not need --, which separated Xen’s options from your program’s arguments.
Continue experimenting
Section titled “Continue experimenting”Accept several paths and print a count for each. Change the error arm in main if you want to continue after a failed file. See stdin_words for a program reading standard input. Extracting word counting into a separate function makes it easier to add tests in the next chapter.