I have this code in Rust:
for ch in string.chars() {
if ch == 't' {
// skip forward 5 places in the string
}
}
In C, I believe you can just do this:
for (int i = 0; i < strlen(string); i) {
if (string[i] == 't') {
i = 4;
continue;
}
}
How would you implement this in Rust? Thanks.
CodePudding user response:
Since string.chars()
gives us an iterator, we can use that to create our own loop that gives us controls about the iterator:
let string = "Hello World!";
let mut iter = string.chars();
while let Some(ch) = iter.next() {
if ch == 'e' {
println!("Skipping");
iter.nth(5);
continue;
}
println!("{}", ch);
}
Will output:
H
Skipping
r
l
d
!