Skip to content

What is the behavior of std::env::split_paths when given an empty string? #111832

Open
@lopopolo

Description

@lopopolo

Location

https://doc.rust-lang.org/nightly/std/env/fn.split_paths.html

Summary

The iterator returned by std::env::split_paths yields a single empty empty string when given a "" or OsStr::new("") argument. Is this by design or a bug?

I found it unexpected that an empty path string yielded any paths at all and was curious if this is a quirk of the implementation using slice::Split on the internals.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a57d0e83edebdcbe70e5e2a54424ee9f

use std::env::split_paths;

fn main() {
    println!("{:?}", split_paths("").collect::<Vec<_>>());
    println!("{:?}", split_paths(":").collect::<Vec<_>>());
    println!("{:?}", split_paths("::").collect::<Vec<_>>());
}

outputs:

[""]
["", ""]
["", "", ""]

The second and third output make sense to me. In the absence of documented behavior, I'm not sure about the first one.

If this is the expected behavior, can it be added to the documentation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-processArea: `std::process` and `std::env`T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions