Skip to main content

get_filename_from_url_or_default

Function get_filename_from_url_or_default 

Source
pub fn get_filename_from_url_or_default(
    url_str: &str,
    default_filename: &str,
) -> String
Expand description

Extract the filename from a URL or return a default.

Parses the URL and returns the last path segment as the filename. If parsing fails or the path is empty, returns the default filename.

§Arguments

  • url_str - URL to extract filename from
  • default_filename - Fallback filename if extraction fails

§Returns

The extracted filename or the default.

§Example

use kget::get_filename_from_url_or_default;

// Successful extraction
assert_eq!(
    get_filename_from_url_or_default("https://example.com/file.zip", "default"),
    "file.zip"
);

// Fallback to default
assert_eq!(
    get_filename_from_url_or_default("https://example.com/", "download.bin"),
    "download.bin"
);