🚀
13. CLI

Mini Grep CLI

use std::env;
use std::fs;
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let query = &args[1];
    let filename = &args[2];
 
    println!("Searching for {:?}", query);
    println!("In file {}", filename);
 
    let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
}
  • env::args returns an iterator of the command-line arguments that were given to the program

  • this code is not safe, it will panic if the user does not provide enough arguments

Using Functions

  • parse_config() function returns a tuple of string slices, which are references to the arguments
use std::env;
use std::fs;
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let (query, filename) = parse_config(&args);
 
    println!("Searching for {:?}", query);
    println!("In file {}", filename);
 
    let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
}
 
fn parse_config(args: &[String]) -> (&str, &str) {
    let query = &args[1];
    let filename = &args[2];
 
    (query, filename)
}

Using Structs

  • Config struct is created to hold the query and filename
use std::env;
use std::fs;
 
struct Config {
    query: String,
    filename: String,
}
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let config: Config = parse_config(&args);
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    let contents =
        fs::read_to_string(config.filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
 
}
 
fn parse_config(args: &[String]) -> Config {
    let query = args[1].clone();
    let filename = args[2].clone();
 
    Config { query, filename }
}

Using Methods

  • new() method is created to initialize the Config struct
use std::env;
use std::fs;
 
struct Config {
    query: String,
    filename: String,
}
 
impl Config {
    fn new(args: &[String]) -> Config {
        let query = args[1].clone();
        let filename = args[2].clone();
 
        Config { query, filename }
    }
}
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let config: Config = Config::new(&args);
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    let contents =
        fs::read_to_string(config.filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
}

Error Handling

  • running the program without enough arguments will panic
$ cargo run
 
thread 'main' panicked at src/main.rs:11:21:
index out of bounds: the len is 1 but the index is 1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\minigrep.exe` (exit code: 101)
  • handling the error by returning a Result from the new() method
use std::env;
use std::fs;
 
struct Config {
    query: String,
    filename: String,
}
 
impl Config {
    fn new(args: &[String]) -> Config {
        if args.len() < 3 {
            panic!("Not enough arguments")
        }
 
        let query = args[1].clone();
        let filename = args[2].clone();
 
        Config { query, filename }
    }
}
 
fn main() {
 
    let args: Vec<String> = env::args().collect();
 
    let config: Config = Config::new(&args);
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    let contents =
        fs::read_to_string(config.filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
 
}
$ cargo run
 
thread 'main' panicked at src/main.rs:12:13:
not enough arguments
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\minigrep.exe` (exit code: 101)
  • method returns Result<Config, &str> instead of Config

  • returning an Err variant if there are not enough arguments

  • wrapping return type with Ok variant

use std::env;
use std::fs;
use std::process;
 
struct Config {
    query: String,
    filename: String,
}
 
impl Config {
    fn new(args: &[String]) -> Result<Config, &str> {
        if args.len() < 3 {
            return Err("Not enough arguments");
        }
 
        let query = args[1].clone();
        let filename = args[2].clone();
 
        Ok(Config { query, filename })
    }
}
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let config: Config = Config::new(&args).unwrap_or_else(|err| {
        println!("Problem parsing arguments: {}", err);
        process::exit(1);
    });
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    let contents =
        fs::read_to_string(config.filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
 
}
$ cargo run
 
Problem parsing arguments: Not enough arguments
error: process didn't exit successfully: `target\debug\minigrep.exe` (exit code: 1)

Implementing main logic outside main()

  • run() function is created to handle the main logic
struct Config {
    // --snip--
}
 
impl Config {
    // --snip--
}
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let config: Config = Config::new(&args).unwrap_or_else(|err| {
        println!("Problem parsing arguments: {}", err);
        process::exit(1);
    });
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    run(config);
 
}
 
fn run(config: Config) {
    let contents =
        fs::read_to_string(config.filename).expect("Something went wrong reading the file");
 
    println!("\n With text: \n\n{}", contents);
}

Graseful Handling Errors

  • unused Result that must be used
  • this Result may be an Err variant, which should be handled
struct Config {
    // --snip--
}
 
impl Config {
    // --snip--
}
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let config: Config = Config::new(&args).unwrap_or_else(|err| {
        println!("Problem parsing arguments: {}", err);
        process::exit(1);
    });
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    if let Err(e) = run(config) {
        println!("Application Error: {}", e);
        process::exit(1);
    }
}
 
fn run(config: Config) -> Result<(), Box<dyn Error>> {
    fs::read_to_string(config.filename)?;
 
    println!("\n With text: \n\n{}", contents);
 
    Ok(())
}
  • Result<(), Box<dyn Error>> is returned from the run() function, which is a generic type that can hold any type of error

  • process::exit(1)

$ cargo run test text.txt
 
Searching for "test"
In file text.txt
Application Error: The system cannot find the file specified. (os error 2)
error: process didn't exit successfully: `target\debug\minigrep.exe test text.txt` (exit code: 1)

Segetating code

main.rs
use std::env;
use std::process;
 
use minigrep::run;
use minigrep::Config;
 
 
fn main() {
    let args: Vec<String> = env::args().collect();
 
    let config: Config = Config::new(&args).unwrap_or_else(|err| {
        println!("Problem parsing arguments: {}", err);
        process::exit(1);
    });
 
    println!("Searching for {:?}", config.query);
    println!("In file {}", config.filename);
 
    if let Err(e) = run(config) {
        println!("Application Error: {}", e);
        process::exit(1);
    }
}
lib.rs
use std::error::Error;
use std::fs;
 
pub struct Config {
    pub query: String,
    pub filename: String,
}
 
impl Config {
    pub fn new(args: &[String]) -> Result<Config, &str> {
        if args.len() < 3 {
            return Err("Not enough arguments");
        }
 
        let query = args[1].clone();
        let filename = args[2].clone();
 
        Ok(Config { query, filename })
    }
}
 
 
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.filename)?;
 
    println!("\n With text: \n\n{}", contents);
 
    Ok(())
}
lib.rs
use std::error::Error;
use std::fs;
 
pub struct Config {
    pub query: String,
    pub filename: String,
}
 
impl Config {
    pub fn new(args: &[String]) -> Result<Config, &str> {
        if args.len() < 3 {
            return Err("Not enough arguments");
        }
 
        let query = args[1].clone();
        let filename = args[2].clone();
 
        Ok(Config { query, filename })
    }
}
 
 
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.filename)?;
 
    for line in search(&config.query, &contents) {
        println!("{}", line);
    }
 
    Ok(())
}
 
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
 
    let mut results = Vec::new();
 
    for line in contents.lines() {
        if line.contains(query) {
            results.push(line);
        }
    }
 
    results
}
 
#[cfg(test)]
mod tests {
    use super::*;
 
    #[test]
    fn one_rusult() {
        let query = "duct"; 
        let contents = "\
Rust:
safe, fast, productive.
Pick three.";
 
        assert_eq!(vec!["safe, fast, productive."], search(query, contents))
    } 
}
lib.rs
use std::error::Error;
use std::fs;
use std::env;
 
pub struct Config {
    pub query: String,
    pub filename: String,
    pub case_sensitive: bool,
}
 
impl Config {
    pub fn new(args: &[String]) -> Result<Config, &str> {
        if args.len() < 3 {
            return Err("Not enough arguments");
        }
 
        let query = args[1].clone();
        let filename = args[2].clone();
        let case_sensitive = env::var("CASE_INSENSITIVE").is_err();
 
        Ok(Config { query, filename, case_sensitive })
    }
}
 
 
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.filename)?;
 
    let results = if config.case_sensitive {
        search(&config.query, &contents);
    } else {
        search_case_insensitive(&config.query, &contents);
    };
 
    for line in search(&config.query, &contents) {
        println!("{}", line)
    }
 
    Ok(())
}
 
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
 
    let mut results = Vec::new();
 
    for line in contents.lines() {
        if line.contains(query) {
            results.push(line);
        }
    }
 
    results
}
 
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
 
    let query = query.to_lowercase();
 
    let mut results = Vec::new();
 
    for line in contents.lines() {
        if line.contains(&query) {
            results.push(line);
        }
    }
 
    results
}
 
#[cfg(test)]
mod tests {
    use super::*;
 
    #[test]
    fn case_sensitive() {
        let query = "duct"; 
        let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
 
        assert_eq!(vec!["safe, fast, productive."], search(query, contents))
    } 
 
    #[test]
    fn case_insensitive() {
        let query = "rUsT";
 
        let contents = "\
        Rust:
        safe, fast, productive.
        Pick three.";
 
        assert_eq!(vec!["Rust:", "Trust me."], search_case_insensitive(query, contents))
    }
}

© 2024 Driptanil Datta.All rights reserved

Made with Love ❤️

Last updated on Mon Oct 20 2025