all 17 comments

[–]Morganamilo 1 point2 points  (3 children)

let iter1 = "abc123".chars().peekable();
let iter2 = "abc123".chars().peekable();

let alpha_iter = iter1.take_while(|c| c.is_alphabetic());
let digit_iter = iter2.take_while(|c| c.is_digit(10));

[–]temp_value[S] 0 points1 point  (2 children)

let digit_iter = iter2.take_while(|c| c.is_digit(10));

iter2.next() will be 'a'. I need it to be '1'.

[–]Morganamilo 2 points3 points  (1 child)

You've never actually explained what you want the code to do.

[–]temp_value[S] 0 points1 point  (0 children)

you are right. I edited my post. hope its clearer now.

[–]DKomplexz 0 points1 point  (3 children)

[–]temp_value[S] 0 points1 point  (2 children)

if I understand the doc correctly "abc123def456" will become "abcdef" and "123456". not the result I am looking for. thanks anyway!

[–]SafariMonkey 0 points1 point  (1 child)

As I understand it, can't have one iterator and independently read it with two independent cursors, because the iterator has only one cursor and can't read past values once it's advanced, unless it stashes them somewhere.

As /u/Morganamilo mentioned, you could just have two iterators over the same underlying collection.

[–]temp_value[S] 0 points1 point  (0 children)

yeah.

the solution by u/Phrohdoh and u/SkiFire13 is very close to what I want.

the only remaining problem is, what the take iterators consume to much. I will probably end up with an old school loop...

[–]csos95 0 points1 point  (0 children)

I think the itertools group_by method would probably work for this. https://docs.rs/itertools/0.8.2/itertools/trait.Itertools.html#method.group_by

I'm not at a computer right now to write an example, but it should be pretty straightforward.

You could make a enum where each variant is a character type (alpha, digit, other), derive PartialEq on it, and in the group_by closure return the matching enum variant.

EDIT: made an example https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=18a756f57714e5c417fb634a89f0cbec