Skip to content

Implement TryFrom<&OsStr> for &str #99031

Closed
@aticu

Description

@aticu

Proposal

Problem statement

Currently it is impossible through use of the standard conversion traits to convert from an &OsStr to a &str.

Motivation, use-cases

Adding an implementation of TryFrom<&OsStr> for &str makes this conversion more discoverable to users who are already familiar with the TryFrom trait. They may even expect such an implementation to exist.
Additionally it allows the use of &OsStr in more generic methods such as those that accept impl TryInto<&str>:

fn maybe_print<'a>(arg: impl TryInto<&'a str>) {
    if let Ok(converted) = arg.try_into() {
        println!("{converted}");
    }
}

Solution sketches

Implement TryFrom<&OsStr> for &str.

Links and related work

Implementation PR: #98202

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UnicodeArea: UnicodeA-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`A-strArea: str and StringT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions