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).

0 Likes

#2

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

2 Likes

#3

To expand on that, why not do

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

#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.

0 Likes

#5

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

0 Likes

closed #6

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

0 Likes