This works well:
//! A simple grep clone written in Rust
#![warn(missing_debug_implementations, rust_2018_idioms, missing_docs)]
use std::{env, error::Error, fs};
/// Configuration for the program
#[derive(Debug)]
pub struct Config<'a> {
/// The string to search for
pub query: &'a str,
/// The path of the file to search
pub filepath: &'a str,
/// Whether to ignore case
pub ignore_case: bool,
}
impl Config<'_> {
/// Creates a new Config from command line arguments
pub fn new(args: &[String]) -> Result<Config<'_>, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = &args[1];
let filepath = &args[2];
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
filepath,
ignore_case,
})
}
}
/// Runs the program
pub fn run(config: &Config<'_>) -> Result<(), Box<dyn Error>> {
let content = fs::read_to_string(config.filepath)?;
let results = if config.ignore_case {
search_case_insensitive(config.query, &content)
} else {
search(config.query, &content)
};
for line in results {
println!("{line}");
}
Ok(())
}
/// Searches for `query` in `contents`
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
contents
.lines()
.filter(|line| line.contains(query))
.collect()
}
/// Searches for `query` in `contents` in a case-insensitive manner
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
contents
.lines()
.filter(|line| line.to_lowercase().contains(&query))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents)
);
}
}
But this doesn't:
//! A simple grep clone written in Rust
#![warn(missing_debug_implementations, rust_2018_idioms, missing_docs)]
use std::{env, error::Error, fs};
/// Configuration for the program
#[derive(Debug)]
pub struct Config<'a> {
/// The string to search for
pub query: &'a str,
/// The path of the file to search
pub filepath: &'a str,
/// Whether to ignore case
pub ignore_case: bool,
}
impl Config<'_> {
/// Creates a new Config from command line arguments
pub fn new(args: &[String]) -> Result<Self, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = &args[1];
let filepath = &args[2];
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Self {
query,
filepath,
ignore_case,
})
}
}
/// Runs the program
pub fn run(config: &Config<'_>) -> Result<(), Box<dyn Error>> {
let content = fs::read_to_string(config.filepath)?;
let results = if config.ignore_case {
search_case_insensitive(config.query, &content)
} else {
search(config.query, &content)
};
for line in results {
println!("{line}");
}
Ok(())
}
/// Searches for `query` in `contents`
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
contents
.lines()
.filter(|line| line.contains(query))
.collect()
}
/// Searches for `query` in `contents` in a case-insensitive manner
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
contents
.lines()
.filter(|line| line.to_lowercase().contains(&query))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents)
);
}
}
The error:
error: lifetime may not live long enough
--> src\lib.rs:30:13
|
20 | pub fn new(args: &[String]) -> Result<Self, &'static str> {
| - -------------------------- return type is Result<Config<'2>, &str>
| |
| let's call the lifetime of this reference `'1`
...
30 | query,
| ^^^^^ this usage requires that `'1` must outlive `'2`
error: could not compile `minigrep` (lib) due to 1 previous error
The difference if renaming Config
to Self