I am on the part where I am trying to add just enough code to my "search" function in my library crate ("src/lib.rs" file) to pass the unit test that checks I am able to grep matching lines based on query_string and contents of file. I do get the single matching line returned from output, but it has too many whitespace characters at beginning, causing it to fail unit test. Much advice is appreciated.
Here is my "src/lib.rs" file.
use std::error::Error;
use std::fs;
pub struct Config {
query: String,
file_path: String
}
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str>{
if args.len() < 3{
return Err("not enough arguments");
}
Ok(Config {query: args[1].clone(), file_path: args[2].clone()})
}
}
pub fn run(config: Config)->Result<(), Box<dyn Error>>{
let contents = fs::read_to_string(config.file_path)?;
Ok(())
}
mod tests {
use super::*;
#[test]
fn one_result() {
let query = "duct";
let contents = "Rust:\n
safe, fast, productive.\n
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
}
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut matching_lines = Vec::new();
for line in contents.lines() {
//check if line contains query substring.
if line.contains(query) {
matching_lines.push(line);
}
}
matching_lines
}
Here is the compiler test results output:
failures:
---- tests::one_result stdout ----
thread 'tests::one_result' panicked at 'assertion failed: `(left == right)`
left: `["safe, fast, productive."]`,
right: `[" safe, fast, productive."]`', src/lib.rs:33:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace