14

Given a relative path:

PathBuf::from("./cargo_home")

Is there a way to get the absolute path?

6

It seems I can't delete this answer while it is accepted. See this answer for a more current and complete answer.

If I understand the PathBuf documentation correctly it does not treat "./" as a special start to a path that says its relative.

You can however turn a relative path into an absolute one with std::env::current_dir:

let relative_path = PathBuf::from("cargo_home");
let mut absolute_path = try!(std::env::current_dir());
absolute_path.push(relative_path)

This assumes that your relative path is relative to your current directory.

  • 3
    This doesn't appear to do quite the same thing as the asker may have intended. If you use this to "absolutise" ./x, you end up with the . as part of the result, whereas they might be expecting it not to show up. The distinction is important if you're trying to get a (within the limits imposed by hard links) canonical path to a file. – DK. May 29 '15 at 14:52
  • 1
    I wouldn't delete your answer; it still can work in some of the conditions that the other answer fails (no file created, for example). – Shepmaster Jan 17 '17 at 19:16
  • How to turn absolute_path into a string for display or format? – Petrus Theron Nov 24 at 9:27
27

Rust 1.5.0 added std::fs::canonicalize, which sounds pretty close to what you want:

Returns the canonical form of a path with all intermediate components normalized and symbolic links resolved.

Note that, unlike the accepted answer, this removes the ./ from the returned path.


A simple example from my machine:

use std::fs;
use std::path::PathBuf;

fn main() {
    let srcdir = PathBuf::from("./src");
    println!("{:?}", fs::canonicalize(&srcdir));

    let solardir = PathBuf::from("./../solarized/.");
    println!("{:?}", fs::canonicalize(&solardir));
}
Ok("/Users/alexwlchan/Developer/so-example/src")
Ok("/Users/alexwlchan/Developer/solarized")
  • 6
    There are a couple problems with canonicalize. 1) It resolves symlinks to a canonical path, so it must access the file system. This can impact performance. 2) It fails if the file does not exist. So you can not use fs::canonicalize() on a path that does not (yet) exist. – Cody Casterline Nov 21 '16 at 16:27
  • 2
    I have run into issues using fs::canonicalize (running on Mac OS X), is it guaranteed that it will return an absolute path, or it just "canonicalizes" the relative path? e.g. "../peer-dir/../second-peer-dir" --> "../second-peer-dir" – Andrew Mackenzie Nov 26 '17 at 8:13
  • Another possible pitfall on Windows: canonicalize (as of Rust 1.31) adds the Extended length path prefix ` \\?\ ` to the path (see docs.microsoft.com/en-us/windows/desktop/FileIO/…). Make sure the consumer of the canonicalized path is aware of that. – Tomáš Dvořák Dec 18 at 13:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.