给定两条绝对路径,如何表达其中一条相对于另一条路径? [英] Given two absolute paths, how can I express one of the paths relative to the other?
问题描述
我认为这应该是非常可行的,因为有一个很好的函数 canonicalize
规范化路径(所以我可以从规范化我的两个输入路径开始)和 Path
和 PathBuf
给我们一种通过 组件
.我想可以在这里解决一些问题来分解出一个公共前缀,然后在锚路径中添加尽可能多的 ..
组件到初始输入路径的剩余部分.
I think this should be quite doable, given that there is a nice function canonicalize
which normalizes paths (so I can start by normalizing my two input paths) and Path
and PathBuf
give us a way of iterating over the parts of paths through components
. I imagine something could be worked out here to factor out a common prefix, then prepend as many ..
components as remain in the anchor path to what remains of the initial input path.
我的问题似乎很常见:
推荐答案
这现在作为 pathdiff
板条箱,使用来自 kennytm 的答案
您可以将其用作:
extern crate pathdiff;
pathdiff::diff_paths(path, base);
其中 base
是应用相对路径来获取 path
where base
is where the relative path should be applied to obtain path
这篇关于给定两条绝对路径,如何表达其中一条相对于另一条路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!