[[!tag learning-rust]]

  • There's a lot of traits just for reading from files, or really anything that implement those traits. This is a bit of maze, and it'll take me a while to learn to navigate this.

  • Do iterators need to be of a particular type or implement a particular trait, or is it enoug to just have a next method that returns an Option?

  • I like that I/O errors must be handled explicitly, and that ? makes that easy. I've tended to become a little complacent with Python's exception handling.

  • I like that filenames have their own type, even if Unix would do with byte strings. So refreshing after Python.

  • We're getting to interesting bits now: doing things with filesystems and processes.

List files program

use std::env;
use std::io;
use std::path::Path;

fn main() -> io::Result<()> {
    for filename in env::args().skip(1) {
        let path = Path::new(&filename);
        find_files(&path)?;
    }
    Ok(())
}

fn find_files(root: &Path) -> io::Result<()> {
    for entry in root.read_dir()? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            find_files(&path)?;
        } else {
            println!("{:?}", entry.path().display());
        }
    }
    Ok(())
}