在 Rust 中将文件名转换为 URI

Convert filename to URI in Rust

在Python中,我们有:

pathlib.Path("~/hello/world").expanduser().as_uri()
# or
pathlib.Path("/hello/world").as_uri()

在 C 中,我们有:

char* uri = g_filename_to_uri("/hello/world", NULL, NULL)
// ...
g_free(uri)

Rust 的等价物是什么?

url crate 中的 from_file_path 方法似乎提供了您想要的功能:

let url = Url::from_file_path("/hello/world").unwrap();
println!("{}", url);  // prints file:///hello/world

Playground