Get the latest tech news
The Magic Function
How do you design user-friendly APIs in Rust? The answer is easy: you use them! Let’s build a simple Rust CLI tool using what I call the “magic function” approach.
When people ask questions like “How should I structure my Rust project?” or “When should I split up my program into multiple modules or crates?”, this is what they’re really asking: what abstractions should I create, and how should they work? So in this excerpt from the book, let’s practise our abstraction skills a little by designing a small but useful command-line tool in Rust: one that can count lines in its input. We’d like to turn this code into a reusable library package that other people can import and use in their own programs: in Rust, this is called a crate.
Or read this on Hacker News