Rust Text Search Tool Tutorial | Find Patterns with Line Numbers | Rust by Examples #9
Video: Rust Text Search Tool Tutorial | Find Patterns with Line Numbers | Rust by Examples #9 by Taught by Celeste AI - AI Coding Coach
Watch full page →Rust Text Search Tool Tutorial | Find Patterns with Line Numbers
In this tutorial, you'll learn how to build a simple text search tool in Rust that reads a file, searches for a pattern case-insensitively, and displays matching lines along with their line numbers. The example demonstrates using Rust's powerful iterator methods for clean and concise code.
Code
use std::fs;
// Struct to hold each search result with line number and content
struct SearchResult {
line_number: usize,
line: String,
}
// Search function: finds all lines containing the pattern (case-insensitive)
fn search(content: &str, pattern: &str) -> Vec<SearchResult> {
let pattern = pattern.to_lowercase();
content
.lines()
.enumerate() // adds line numbers starting at 0
.filter_map(|(idx, line)| {
// Check if line contains pattern (case-insensitive)
if line.to_lowercase().contains(&pattern) {
Some(SearchResult {
line_number: idx + 1, // line numbers start at 1
line: line.to_string(),
})
} else {
None
}
})
.collect()
}
fn main() {
// Read the entire file into a string
let content = fs::read_to_string("sample.txt").expect("Failed to read file");
let pattern = "rust";
// Perform the search
let results = search(&content, pattern);
println!("Found {} matches for '{}':", results.len(), pattern);
// Display each matching line with its line number
for result in results {
println!("{}: {}", result.line_number, result.line);
}
}
Key Points
- Use
fs::read_to_stringto read an entire file into a string for processing. - Split text into lines with
lines()and pair lines with their indices usingenumerate(). - Perform case-insensitive searches by converting both the pattern and lines to lowercase before matching.
- Use iterator adapters like
filter_map()to filter and transform lines that match the search pattern. - Collect matching results into a vector of structs to keep track of line numbers and content for easy display.