0
0
Rustprogramming~20 mins

Boolean type in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Boolean Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code?

Consider the following Rust program. What will it print?

Rust
fn main() {
    let a = true;
    let b = false;
    println!("{}", a && b || !b);
}
Atrue
BCompilation error
Cfalse
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint

Remember the precedence of logical operators: ! has highest, then &&, then ||.

โ“ Predict Output
intermediate
2:00remaining
What is the value of variable x after this code runs?

Look at this Rust code snippet. What is the value of x after execution?

Rust
fn main() {
    let x = if true { 10 } else { 20 };
    println!("{}", x);
}
ACompilation error
B20
Ctrue
D10
Attempts:
2 left
๐Ÿ’ก Hint

The if expression returns the value of the branch taken.

โ“ Predict Output
advanced
2:00remaining
What does this Rust code print?

Analyze this Rust code and choose the output it produces.

Rust
fn main() {
    let a = true;
    let b = false;
    let c = a ^ b;
    println!("{}", c);
}
Atrue
Bfalse
CCompilation error: no operator ^ for bool
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint

The ^ operator on booleans means XOR (exclusive or).

โ“ Predict Output
advanced
2:00remaining
What error does this Rust code produce?

What error will this Rust code cause when compiled?

Rust
fn main() {
    let x: bool = 1;
    println!("{}", x);
}
ARuntime panic
BCompilation succeeds and prints true
CType mismatch error
DSyntax error
Attempts:
2 left
๐Ÿ’ก Hint

Rust is strict about types and does not allow implicit conversion from integers to booleans.

๐Ÿง  Conceptual
expert
2:00remaining
How many items are in the resulting vector after filtering?

Consider this Rust code that filters a vector of booleans. How many items remain after filtering?

Rust
fn main() {
    let values = vec![true, false, true, false, false, true];
    let filtered: Vec<bool> = values.into_iter().filter(|&x| x).collect();
    println!("{}", filtered.len());
}
A4
B3
C6
D0
Attempts:
2 left
๐Ÿ’ก Hint

The filter keeps only true values.