I tried implementing this as a macro. (link)
This can then be used like this:
fn main() {
let mut list = String::new();
custom_loop!(
for a in 0..11; { // `;` needed because of macro limitation
list.push_str(&format!("{}", a));
} between {
list.push_str(", ");
} then {
list.push_str(" END")
}
);
println!("{}", list); // "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 END"
}
And combining all features
fn main() {
let mut list = String::new();
custom_loop!(
for a in 0..11; {
if a > 5 {
break
}
list.push_str(&format!("{}", a));
} between {
list.push_str(", ");
} then {
list.push_str(" END")
} else {
list.push_str("EARLYEND")
}
);
println!("{}", list); // "0, 1, 2, 3, 4, 5, EARLYEND"
}
and that expands to this:
fn main() {
let mut list = String::new();
{
let mut _normal = true;
let mut iter = IntoIterator::into_iter(0..11);
let a = iter.next();
if a.is_some() {
_normal = false;
let mut a = a.unwrap();
loop {
if a > 5 {
break
}
list.push_str(&format!("{}", a));
a = match iter.next() {
Some(x) => {
list.push_str(", ");
x
}
None => {
_normal = true;
break
}
};
}
}
if _normal {
list.push_str("END")
} else {
list.push_str("EARLYEND")
}
}
println!("{}", list);
}
Should be useful if someone wants to try out this feature.