rust-lang / libs-team

The home of the library team
Apache License 2.0
127 stars 19 forks source link

ACP: Chars::take_str #449

Open dev-ardi opened 2 months ago

dev-ardi commented 2 months ago

Proposal

A fn in Chars<'a> that skips until it finds a specific char and returns the &'a str while it was skipping.

For example:

"I like bananas"
    ^ Chars is here 
let subslice = my_chars.take_str(' ');
assert_eq(subslice, "like");
"I like bananas"
          ^ Chars is here 

Problem statement

There's currently no ergonomic way of doing this, it's possible but not something that feels supported.

Motivating examples or use cases

While iterating over chars it's not the most ergonomic thing to find individual words, or to get subslices.

Solution sketch

fn take_str(&mut self) -> &str
BurntSushi commented 2 months ago

Please fill out the motivation section more. I would like to see a real example of its use.

It also looks like your prose description of its behavior doesn't match the behavior shown in your code snippet.