跳到主要内容

canonicalize

搜索

函数 canonicalize 

Source
pub async fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf>
展开描述

返回路径的规范化绝对形式,所有中间组件均已规范化,符号链接已解析。

这是 std::fs::canonicalize.

§Platform-specific behavior

此函数当前对应于 realpath function on Unix and the CreateFile and GetFinalPathNameByHandle functions on Windows. 请注意,此 将来可能会发生变化.

在 Windows 上,这会将路径转换为使用 extended length path syntax, which allows your program to use longer path names, but means you can only join backslash-delimited paths to it, and it may be incompatible with other applications (if passed to the application on the command-line, or written to a file another application may read).

§Errors

在以下情况下此函数将返回错误,但不限于以下这些情况:

  • path does not exist.
  • A non-final component in path is not a directory.

§示例

use tokio::fs;
use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let path = fs::canonicalize("../a/../foo.txt").await?;
    Ok(())
}