Minor RFC: Path::from_string


#1

Right now, creating a Path from a temporary String is a two-liner:

fn create_temporary_string() -> String { ... }

let path_str = create_temporary_string();
let path = Path::new(&path_str);

Would there be interest in adding the following, or is it two much clutter to avoid an extra line of user code?

let path = Path::from_string(create_temporary_string());

If there’s interest, I’ll go ahead and create a PR (I presume this doesn’t need a full RFC).


#2

I might be missing something, but given that Path is a borrowed type, how can this possibly work?


#3

To expand on that, why not do

let path = PathBuf::from(create_temporary_string());

#4

@dhardy I assume you’re proposing this due to the lifetime problem with

let path = Path::new(&create_temporary_string());

@DanielKeep is right that Path::from_string is not possible to implement, and @gkoz’s alternative is correct.


#5

I didn’t realise that was how Path works. Thanks for the corrections.