Skip to content

Tracking issue for Path::relative_from stabilization #23284

Closed

Description

@brson

Path::relative_from removes the common prefix from a path, e.g. /foo/bar relative_from /foo yields bar.

The old path_relative_from would calculate the relative path from two points on the file system, e.g. the relative path from /foo/bar to /foo/baz/qux is ../baz/qux. This is a useful operation.

Nominating because I think either relative_from is named wrong or is not useful.

Activity

  1. brson commented on Mar 11, 2015

    @brson
    ContributorAuthor
  2. aturon commented on Mar 12, 2015

    @aturon
    Contributor

    cc me

  3. aturon commented on Mar 12, 2015

    @aturon
    Contributor

    Note that the reason the new semantics was adopted has to do with symlinks (the same reason we do not normalize ..). We'll need to discuss this further, but for now it's plausible to leave this function unstable.

  4. pnkfelix commented on Mar 19, 2015

    @pnkfelix
    Contributor

    Function is not yet stable, so this need not be milestoned. P-high, not 1.0.

  5. lilyball commented on Mar 23, 2015

    @lilyball
    Contributor

    Path::relative_from made more sense back when we auto-normalized paths. The idea was if you joined the result to the first path, you'd get the second path. Since we don't normalize like that anymore, that behavior doesn't work.

    It's still a potentially useful operation, but because of symlinks, there are two different behaviors that might be desired:

    1. The current behavior where joining the result onto the first path unambiguously refers to the same thing the second path does, even if there's symlinks (which basically means base needs to be a prefix of self)
    2. The old behavior where the result can start with ../ components. Symlinks mean traversing the base path and then traversing the returned relative path may not put you in the same directory that traversing theself` path does. But this operation is useful when either you're working with a path-based system that doesn't care about symlinks, or you've already resolved symlinks in the paths you're working with.

    Based on that I think it's reasonable to provide both implementations. The former might want to be called something like strip_prefix() instead. the latter could continue to be called relative_from.

  6. changed the title [-]New Path::relative_from is less useful than old Path::path_relative_from[/-] [+]Tracking issue for Path::relative_from stabilization[/+] on Aug 12, 2015
  7. added
    T-libs-api[DEPRECATED; DO NOT USE]
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    on Aug 12, 2015
  8. withoutboats commented on Aug 23, 2015

    @withoutboats
    Contributor

    Nominating because I think either relative_from is named wrong or is not useful.

    Just noting that I have found this function useful. I think it would probably make more sense to name it something like strip_prefix though.

  9. rgardner commented on Nov 6, 2015

    @rgardner
    Contributor

    I too have found this function useful. What has to happen to stabilize this?

  10. JanLikar commented on Nov 15, 2015

    @JanLikar

    This should be stabilized or a similar function should be proposed.

  11. qmx commented on Nov 30, 2015

    @qmx
    Contributor

    since @mbrubeck pointed me here for my usecase too, here it comes:

    let path = Path::new("/might/be/something/a");
    assert_eq!(path.relative_from(Path::new("/might/be")), Path::new("something/a"));

    I'm more than fine on renaming it to strip_prefix.

  12. 13 remaining items

  13. BurntSushi commented on Jan 13, 2016

    @BurntSushi
    Member

    I like strip_prefix. Returning a Result seems prudent.

  14. aturon commented on Jan 13, 2016

    @aturon
    Contributor

    I'm on board with both the renaming and using Result here.

  15. alexcrichton commented on Jan 15, 2016

    @alexcrichton
    Member

    The libs team discussed this during triage yesterday and the decision was to stabilize with the name strip_prefix and the tweak of returning a Result.

  16. softprops commented on Jan 16, 2016

    @softprops

    Is strip_prefix a different name for the same behavior found on strings?
    https://doc.rust-lang.org/std/string/struct.String.html#method.trim_left_matches

  17. SimonSapin commented on Jan 16, 2016

    @SimonSapin
    Contributor

    @softprops str::trim_left_matches zero or more instances of the pattern, as many as possible. Path::strip_prefix removes exactly one or returns Err if there isn’t a match. It also only cuts at path separators.

  18. added a commit that references this issue on Jan 17, 2016
    80e21d1
  19. added a commit that references this issue on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.P-mediumMedium priorityT-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions