Function camino::absolute_utf8

source ·
pub fn absolute_utf8<P: AsRef<Path>>(path: P) -> Result<Utf8PathBuf>
Expand description

Makes the path absolute without accessing the filesystem, converting it to a Utf8PathBuf.

If the path is relative, the current directory is used as the base directory. All intermediate components will be resolved according to platform-specific rules, but unlike canonicalize or canonicalize_utf8, this does not resolve symlinks and may succeed even if the path does not exist.

Requires Rust 1.79 or newer.

§Errors

Errors if:

  • The path is empty.
  • The current directory cannot be determined.
  • The path is not valid UTF-8.

§Examples

§POSIX paths

fn main() -> std::io::Result<()> {
    use camino::Utf8Path;

    // Relative to absolute
    let absolute = camino::absolute_utf8("foo/./bar")?;
    assert!(absolute.ends_with("foo/bar"));

    // Absolute to absolute
    let absolute = camino::absolute_utf8("/foo//test/.././bar.rs")?;
    assert_eq!(absolute, Utf8Path::new("/foo/test/../bar.rs"));
    Ok(())
}

The path is resolved using POSIX semantics except that it stops short of resolving symlinks. This means it will keep .. components and trailing slashes.

§Windows paths

fn main() -> std::io::Result<()> {
    use camino::Utf8Path;

    // Relative to absolute
    let absolute = camino::absolute_utf8("foo/./bar")?;
    assert!(absolute.ends_with(r"foo\bar"));

    // Absolute to absolute
    let absolute = camino::absolute_utf8(r"C:\foo//test\..\./bar.rs")?;

    assert_eq!(absolute, Utf8Path::new(r"C:\foo\bar.rs"));
    Ok(())
}

For verbatim paths this will simply return the path as given. For other paths this is currently equivalent to calling GetFullPathNameW.

Note that this may change in the future.