Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

str::array_split for splitting a string into a constant number of substrings #102905

Open
pitaj opened this issue Oct 11, 2022 · 6 comments
Open
Labels
A-array Area: `[T; N]` A-const-generics Area: const generics (parameters and arguments) A-str Area: str and String T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@pitaj
Copy link
Contributor

pitaj commented Oct 11, 2022

Related to:

The idea is to provide an API similar to split_once, but allowing the programmer to specify any number of substrings at compile time, instead of being limited to two (one split) or relying on splitn at runtime.

This is helpful for many cases where a programmer wants exactly N substrings, no more and no less, and knows N at compile time. splitn can work for this use case, but since you get an iterator out, there's no concise way to get the substrings into an array. You could also have less than N substrings.

API

pub fn array_split<const N: usize, 'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<[&'a str; N]>

Usage example

let static_format = "abc 123 xyz The rest of the string";
let parts: [&str; 4] = static_format.array_split(char::is_whitespace).unwrap();
// or using turbofish to specify # of substrings
let parts = static_format.array_split::<4>(char::is_whitespace).unwrap();

assert_eq!(parts, ["abc", "123", "xyz", "The rest of the string"]);

Returning an array enables nice features like infallible destructuring:

let line = "John Doe,Engineer,Kansas,555-555-5555,This is a comment about John, who is a hard worker, and loves to fish.";
let [name, occupation, state, phone_num, comment] = line.array_split().unwrap();

assert_eq!(name, "John Doe");
assert_eq!(phone_num, "555-555-5555");
assert_eq!(comment, "This is a comment about John, who is a hard worker, and loves to fish.");

@rustbot label +T-libs-api +A-str +A-array +A-const-generics

@rustbot rustbot added A-array Area: `[T; N]` A-const-generics Area: const generics (parameters and arguments) A-str Area: str and String T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 11, 2022
@pitaj pitaj changed the title str::array_split for splitting a string into a constant number of parts str::array_split for splitting a string into a constant number of substrings Oct 11, 2022
@camsteffen
Copy link
Contributor

I think it would be better to have a good solution for #81615.

@pitaj
Copy link
Contributor Author

pitaj commented Oct 20, 2022

That would still require some new associated string function to return a const generic iterator, right?

@camsteffen
Copy link
Contributor

I think str::split would work since it's lazy.

@pitaj
Copy link
Contributor Author

pitaj commented Oct 21, 2022

But str::split would not give you the rest of the string as the last element.

@camsteffen
Copy link
Contributor

How about Split::as_str?

@pitaj
Copy link
Contributor Author

pitaj commented Oct 21, 2022

Sure, but that requires either special handling in the FromIterator (or whatever) adapter, or manual handling by the user. The first is unlikely to be included in std, and the second is not ergonomic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-array Area: `[T; N]` A-const-generics Area: const generics (parameters and arguments) A-str Area: str and String T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants