Sorting /etc/passwd

use std::fs;
use anyhow::Result;

#[derive(Debug)]
struct Entry {
    username: String,
    uid: u16,
    gid: u16
}

fn main() -> Result<()> {
    let password_file = "/etc/passwd";

    let content = fs::read_to_string(password_file)?;
    let mut entries = content
        .split('\n')
        .filter(|entry| entry.split(':').count() >= 4)
        .map(|entry| {
            let values = entry.split(':').collect::<Vec<&str>>();
            Entry {
                username: values[0].to_string(),
                uid: values[2].parse::<u16>().unwrap(),
                gid: values[3].parse::<u16>().unwrap(),
            }
        })
        .collect::<Vec<Entry>>();

    entries.sort_by(|a, b| a.uid.cmp(&b.uid));

    for entry in entries {
        println!("{username} ({uid},{gid})",
            username=entry.username,
            uid=entry.uid,
            gid=entry.gid,
        )
    }

    Ok(())
}

More on this: