0
fn get_filename_from_url(url: &str, extension: &str) -> str {
    let dt = chrono::Local::now();
    let cleaned_url = url
        .replace("https://", "")
        .replace("/", "-")
        .replace("?", "-")
        .as_str();
    return format!(
        "{}{}_{}{}",
        OUTPUT_FOLDER,
        cleaned_url,
        dt.format("%Y-%m-%d_%H.%M.%S"),
        extension
    )
    .as_str();
}

I get errors such as:

error[E0106]: missing lifetime specifier
  --> src/scraper.rs:14:57
   |
14 | fn get_filename_from_url(url: &str, extension: &str) -> &str {
   |                               ----             ----     ^ expected named lifetime parameter

What am I doing wrong?

P.S. I was also interested to see Is there a safe / sanitised filename function in Rust but didn't see a helpful example.

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 1
    I notrice that the return type in the error message does not match the code in your snippet – `str` vs `&str`. I assume that you are asking about the code in the error message. – Sven Marnach Jun 29 '22 at 20:15
  • 1
    I just realize that your problem is something else. You should return a `String`, and remove the `as_str()` at the end of the function. I'll add a better duplicate. – Sven Marnach Jun 29 '22 at 20:20
  • I've added another duplicate. Let me know whether this answers your questions. – Sven Marnach Jun 29 '22 at 20:22
  • @SvenMarnach I couldn't figure out it out from the links, but your comment helped me. Thanks. This works: `fn get_filename_from_url(url: &str, extension: &str) -> String { let dt = chrono::Local::now(); let cleaned_url = url .replace("https://", "") .replace("/", "-") .replace("?", "-"); return format!( "{}{}_{}{}", OUTPUT_FOLDER, cleaned_url, dt.format("%Y-%m-%d_%H.%M.%S"), extension ); }` – Ryan Jun 29 '22 at 20:27

0 Answers0