Crates.io | do_while |
lib.rs | do_while |
version | 0.1.0 |
source | src |
created_at | 2022-12-23 19:17:35.338088 |
updated_at | 2022-12-23 19:17:35.338088 |
description | A simple macro allowing clean 'do-while' loops in Rust |
homepage | https://github.com/arthomnix/do-while |
repository | https://github.com/arthomnix/do-while |
max_upload_size | |
id | 744685 |
size | 8,604 |
A simple Rust macro for wriing clean 'do-while' loops.
A standard do-while loop:
let mut x = 0;
do_while! {
do {
x += 1;
} while x < 10;
}
assert_eq!(x, 10);
'Do-while-do' loops are also supported, running a block of code before the condition is evaluated and another block after the condition is evaluated. This is useful for things like formatting lists:
let items = vec![1, 2, 3, 4];
let mut string = String::new();
let mut index: usize = 0;
do_while! {
do {
string.push(items[index].to_string);
index += 1;
} while index < items.len(), do {
string.push(", ");
}
}
assert_eq!(string, "1, 2, 3, 4".to_string());
Multiple do-while and do-while-do loops can be mixed and matched in the same macro invocation:
let mut x = 0;
let mut y = 0;
let list = vec![5, 6, 7, 8];
let mut string = String::new();
let mut index: usize = 0;
do_while! {
do {
x += 1;
} while x < 10;
do {
y -= 1;
} while y > -20;
do {
string.push_str(&list[index].to_string());
index += 1;
} while index < list.len(), do {
string.push_str(", ");
}
}
assert_eq!(x, 10);
assert_eq!(y, -20);
assert_eq!(string, "5, 6, 7, 8".to_string());