Build a practical CLI log inspector
Process command-line input and filter operational logs.
- Parse CLI arguments and report matching log entries.
Production teams rely on small CLI tools for support and operations. In Rust, std::env::args is enough for basic parsing, while crates like clap help build polished interfaces as tools grow.
1use std::env;
2
3fn main() {
4 let args: Vec<String> = env::args().collect();
5 if args.len() < 2 {
6 println!("usage: logtool <keyword>");
7 return;
8 }
9
10 let keyword = &args[1];
11 let sample = [
12 "INFO service started",
13 "WARN latency above threshold",
14 "ERROR database timeout",
15 ];
16
17 for line in sample {
18 if line.contains(keyword) {
19 println!("{}", line);
20 }
21 }
22}ERROR database timeout
A focused CLI can be shipped as a single binary and used in CI pipelines, incident response, and developer workflows. This is one of Rust’s strongest real-world entry points.
Try it
Up to 15 XPMap input to CLI behavior
Sort each runtime situation by the expected CLI behavior.
“No keyword passed on the command line”
“Keyword `ERROR` is provided”
“Keyword `WARN` is provided”
“Arguments list only contains program name”
Key takeaways
Parse CLI arguments and report matching log entries.
Rust uses types and ownership rules to prevent common bugs.
Small experiments build intuition quickly.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Rust
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning Rust.
Filter error lines from stdin
Read one keyword, then one line containing |-separated log entries. Print entries that contain the keyword; print none if there are no matches.
- Single match
- No matches
Rust lessons are authored for modern stable Rust; exercises run on Rust 1.82 via Wandbox, a free public service. Your source code and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…