for-else

Crates.iofor-else
lib.rsfor-else
version0.2.0
sourcesrc
created_at2023-09-01 15:38:57.467385
updated_at2023-09-03 12:48:33.410207
descriptionPython-esque for-else construct for Rust
homepage
repository
max_upload_size
id961003
size12,016
Aleksandr Kovalev (sanchopanca)

documentation

README

for-else

Python-esque for-else construct for Rust

Overview

The for-else library introduces a procedural macro, for_!, that extends the capabilities of the standard for loop in Rust. This enhancement allows for an else block to be added directly within the loop, which is executed only if the loop completes without hitting a break statement, closely mirroring Python's for-else construct.

Usage

First, add the dependency to your Cargo.toml:

cargo add for-else

Then, use the macros in your code:

use for_else::for_;

// not the best way to test primality, just for demonstration
fn is_prime(n: u32) -> bool {
    if n <= 1 {
        return false;
    }
    for i in 2..n {
        if n % i == 0 {
            return false;
        }
    }
    true
}

for_! { n in 2100..=2110 {
    if is_prime(n) {
        println!("Found a prime number: {}", n);
        break;
    }
} else {
    println!("No prime numbers found in the range.");
}}

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.

License

MIT

Commit count: 0

cargo fmt