admin管理员组文章数量:1416321
MRE:
let mut ws = s.split_whitespace();
for w in ws {
if w == "option1" { //do something }
else if w == "option2" { //do something else }...
// this is the tricky part
else if w == "{" { // call the function recursively to process the inner set of 'w's but pass it 's' because the for loop destroys s as it reads through it }
}
Is there a way to achieve this is rust? I am assuming I would have to use a more complex for loop than a simple "foreach iterative loop" and would have to do something on the lines of: for i in 0..len(ws)
and then somehow dynamically change ws
so its length changes?
MRE:
let mut ws = s.split_whitespace();
for w in ws {
if w == "option1" { //do something }
else if w == "option2" { //do something else }...
// this is the tricky part
else if w == "{" { // call the function recursively to process the inner set of 'w's but pass it 's' because the for loop destroys s as it reads through it }
}
Is there a way to achieve this is rust? I am assuming I would have to use a more complex for loop than a simple "foreach iterative loop" and would have to do something on the lines of: for i in 0..len(ws)
and then somehow dynamically change ws
so its length changes?
1 Answer
Reset to default 7Are you looking for something like this
fn handle_token<'a>(tokens: &mut impl Iterator<Item = &'a str>) {
while let Some(t) = tokens.next() {
match t {
"foo" => println!("foo"),
"bar" => println!("bar"),
"{" => {
println!(">> enter");
handle_token(tokens);
println!("<< exit");
},
"}" => {
break;
}
_=> unimplemented!()
}
}
}
fn main() {
let mut ws = "foo bar { foo bar } bar".split_whitespace();
handle_token(&mut ws);
}
foo
bar
>> enter
foo
bar
<< exit
bar
本文标签: How do I desctructively iterate over an array of strings in rustStack Overflow
版权声明:本文标题:How do I desctructively iterate over an array of strings in rust? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745251072a2649825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论