Rust Program to Check if two of three boolean variables are true


Check if two of three boolean variables are true

Rust Programming Language


Check if two of three boolean variables are true


Problem


Rust program that checks if two out of three boolean variables are true.

Input


fn main() {
    let a = true;
    let b = false;
    let c = true;

    let count = [a, b, c].iter().filter(|&x| *x).count();

    if count == 2 {
        println!("Two out of three variables are true");
    } else {
        println!("Less than or more than two variables are true");
    }
}{codeBox}


Output


Two out of three variables are true{codeBox}


Explanation


In this program, we define three boolean variables a, b, and c, and then create an array containing these variables. We use the iter() method to create an iterator over the array, and then the filter() method to keep only the elements that are true. Finally, we use the count() method to count the number of elements in the filtered iterator.

If the count is equal to 2, we print a message indicating that two out of three variables are true; otherwise, we print a message indicating that less than or more than two variables are true.

Post a Comment