I am trying to parse a particular string which has format similar to this:
prefix,body1:body2
I would like to use .chars
method and other methods like .take_while
and others like this:
let chars = str.chars();
let prefix: String = chars.take_while(|&c| c != ',').collect();
let body1: String = chars.take_while(|&c| c != ':').collect();
let body2: String = chars.take_while(|_| true).collect();
But the compiler complains:
error: use of moved value: `chars` [E0382]
let body1: String = chars.take_while(|&c| c != ':').collect();
^~~~~
help: see the detailed explanation for E0382
note: `chars` moved here because it has type `core::str::Chars<'_>`, which is non-copyable
let prefix: String = chars.take_while(|&c| c != ',').collect();
^~~~~
I can rewrite it to a plain for
loop and accumulate the value, but this is something I would like to avoid.