for-else
Python-esque for-else construct for Rust
Overview
The for-else library introduces two procedural macros, for_! and while_!, that extend the capabilities of the standard loops in Rust.
This enhancement allows for an else block to be added directly after the loop,
which is executed only if the loop completes without hitting a break statement, closely mirroring Python's for-else and while-else constructs.
Usage
First, add the dependency to your Cargo.toml:
cargo add for-else
Then, use the macros in your code:
use for_;
// not the best way to test primality, just for demonstration
for_!
In this example, the program searches for the first prime number in the range [2100, 2110]. If a prime is found, it prints the number. If no prime is found, the else block within the for_! macro executes, notifying the user.
Documentation
For detailed information on each macro and its behavior, please refer to the documentation
Contributing
Contributions are always welcome! Please open an issue or submit a pull request.