Learning rust - part 2

This is an unpublished post. It's likely (more) full of spelling mistakes and errors. It may also end abruptly without a conclusion.

Painful

A search function

fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    contents
        .lines()
        .filter(|line| line.contains(query))
        .collect()
}

I wanted a case insensitive one. I thought this would work:

fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    let lowercase_contents = contents.to_lowercase();
    let lowercase_search = query.to_lowercase();
    search(&lowercase_search, &lowercase_contents)
}

but I got:

error[E0515]: cannot return value referencing local variable `lowercase_contents`
  --> src/lib.rs:45:5
   |
45 |     search(&lowercase_search, &lowercase_contents)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^
   |     |                         |
   |     |                         `lowercase_contents` is borrowed here
   |     returns a value referencing data owned by the current function

my hacky fix:

fn search(query: &str, contents: String) -> Vec<String> {
    contents
        .lines()
        .filter(|line| line.contains(query))
        .map(|s| s.to_string())
        .collect()
}

fn search_case_insensitive(query: &str, contents: String) -> Vec<String> {
    let lowercase_contents = contents.to_lowercase();
    let lowercase_search = query.to_lowercase();
    search(&lowercase_search, lowercase_contents)
}
Thoughts? Comments? Contact me on mastodon!