path_to_string

Function path_to_string 

Source
pub fn path_to_string(path: &Path) -> String
Expand description

Safely converts a path to a string, handling non-UTF-8 paths gracefully.

This function converts a Path to a String using lossy conversion, which means invalid UTF-8 sequences are replaced with the Unicode replacement character (�). This ensures the function never panics.

§Arguments

  • path - The path to convert to a string

§Returns

A string representation of the path (may contain replacement characters)

§Examples

use agpm_cli::utils::platform::path_to_string;
use std::path::Path;

let path = Path::new("/home/user/file.txt");
let path_str = path_to_string(path);
println!("Path as string: {}", path_str);

§Platform Considerations

  • Windows: Paths are typically valid UTF-16, so conversion is usually lossless
  • Unix-like: Paths can contain arbitrary bytes, so lossy conversion may occur
  • All platforms: This function never panics, unlike direct UTF-8 conversion

§Use Cases

  • Logging and error messages
  • Display to users
  • Interfacing with APIs that expect strings
  • JSON serialization of paths

§Alternative

For cases where you need OsStr (which preserves all path information), use path_to_os_str instead.

§See Also