#path #relative #absolutize #home-dir #add-path


A small path tool works for calculate absolutive or relative path

2 releases

0.1.3 Feb 2, 2021
0.1.2 Jan 29, 2021
0.1.1 Jan 27, 2021
0.1.0 Jan 27, 2021

#597 in Filesystem

Download history 143/week @ 2023-08-13 56/week @ 2023-08-20 198/week @ 2023-08-27 120/week @ 2023-09-03 109/week @ 2023-09-10 73/week @ 2023-09-17 72/week @ 2023-09-24 140/week @ 2023-10-01 161/week @ 2023-10-08 132/week @ 2023-10-15 249/week @ 2023-10-22 233/week @ 2023-10-29 450/week @ 2023-11-05 273/week @ 2023-11-12 293/week @ 2023-11-19 169/week @ 2023-11-26

1,219 downloads per month
Used in fontpm

MIT license

108 lines

Path Calculate

This is a library help you to calculate with Path or PathBuf, such as get absolute path, get the relate root or the relative path between two pathes, get the '~'(home_dir) if it exist.

This is based on path_absolutize library, I've use as_absolute_path replace the row absolutize, because it do not support '~' which is use recently, at least in UNIX System.

The following examples show the usage about this crate.


There are some methods you can use.


Get the current user's HOME if it exist in your env, or it return an error(I'm lazy, so just put this method in the Calculate trait).

extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

let p = Path::new("/tmp");

if let Ok(home_dir) = p.home_dir() {
    println!("Home path: {:?}", home_dir);


This is almost like a shadow of the absolutize in path-absolutize, I only add ~($HOME) support in the method(Unix or Windnows).

extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

// If u have $HOME, test `~` support
let p = Path::new("./whatever");
if let Ok(home_dir) = p.home_dir() {
    let p = Path::new("~");
    assert_eq!(home_dir.to_str().unwrap(), p.as_absolute_path().unwrap().to_str().unwrap());

let p2 = Path::new("/tmp/a/b/c/../../e/f");

assert_eq!("/tmp/a/e/f", p2.as_absolute_path().unwrap().to_str().unwrap());


Sometimes I would use the relative root of pathes, it return an absolutize relative_root path. Behold, in Windows, it can not calculate the relative_root on different disks(C:\, D:\,,,), when you try to use this method it return an ioerror such as io::ErrorKind::InvalidInput.

extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

let p1 = Path::new("/home/gits/mkisos");
let p2 = Path::new("/home/cc/trash");

let relative_root = p1.relative_root_with(&p2);

assert_eq!("/home", relative_root.unwrap().to_str().unwrap())
extern crate path_calculate;

use std::io::ErrorKind;
use std::path::Path;

use path_calculate::*;

// Pass Test when in Unix
if cfg!(target_os = "windows") {
    // Windows ok
    let d1 = Path::new("D:\\Games\\Videos\\Replays");
    let d2 = Path::new("D:\\Games\\Dota2");
    assert_eq!("D:\\Games", d1.relative_root_with(&d2).unwrap().to_str().unwrap());
    // Windows err
    let c1 = Path::new("~");

    assert_eq!(ErrorKind::InvalidInput, c1.relative_root_with(&d1).unwrap_err().kind());

This method is used to calculate the dst_path's relative path from the src_path.

extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

// $HOME="/home/chao"
let dst_path = Path::new("/home/chao/works/demo/src");
let src_path = Path::new("/home/chao/trash");

assert_eq!("../works/demo/src", dst_path.related_to(&src_path).unwrap().to_str().unwrap());


use path.join();