An if expression lets your program choose what to do based on a condition. It helps your code make decisions like a fork in the road.
If expression in Rust
if condition { // code if condition is true } else { // code if condition is false }
The condition must be a boolean (true or false).
In Rust, if is an expression, so it returns a value.
x is greater than zero.if x > 0 { println!("x is positive"); }
x is even or odd and prints a message.if x % 2 == 0 { println!("x is even"); } else { println!("x is odd"); }
if as an expression to assign a value to sign.let sign = if x < 0 { "negative" } else { "non-negative" };
This program checks if x is positive, negative, or zero and prints a message. Then it uses an if expression to find if x is even or odd and prints that.
fn main() {
let x = 7;
if x > 0 {
println!("{} is positive", x);
} else if x < 0 {
println!("{} is negative", x);
} else {
println!("{} is zero", x);
}
let description = if x % 2 == 0 { "even" } else { "odd" };
println!("{} is {}", x, description);
}Remember, the condition must always be a boolean; Rust does not allow numbers or other types as conditions.
You can chain multiple else if blocks to check many conditions.
Because if is an expression, you can assign its result directly to a variable.
If expressions let your program choose what to do based on true or false conditions.
You can use if with else and else if to handle multiple cases.
In Rust, if returns a value, so you can assign it to variables.
Practice
if expression do in Rust?Solution
Step 1: Understand the purpose of
Theififexpression checks a condition to decide which code block to run.Step 2: Compare with other options
Repeating code is done by loops, functions define behavior, and variables store data, so these are incorrect.Final Answer:
It chooses between code blocks based on a condition. -> Option AQuick Check:
If expression = choose code [OK]
- Confusing if with loops
- Thinking if declares variables
- Mixing if with function definitions
if expression in Rust?Solution
Step 1: Recall Rust if syntax
Rust usesif condition { ... } else { ... }without parentheses or arrows.Step 2: Check each option
if condition { /* code */ } else { /* code */ } matches Rust syntax; others use invalid keywords or symbols.Final Answer:
if condition { /* code */ } else { /* code */ } -> Option BQuick Check:
Rust if syntax = braces, no parentheses [OK]
- Adding parentheses like other languages
- Using 'then' keyword
- Using colons or arrows incorrectly
let x = 10;
let y = if x > 5 { "big" } else { "small" };
println!("{}", y);Solution
Step 1: Evaluate the condition
Sincex > 5xis 10, which is greater than 5, the condition is true.Step 2: Determine which branch runs
Theifexpression returns "big" because the condition is true, soyis assigned "big".Final Answer:
big -> Option CQuick Check:
10 > 5 is true, so output = big [OK]
- Confusing output with variable x
- Thinking else branch runs
- Expecting numeric output
let number = 7;
let result = if number % 2 == 0 {
"even"
} else {
"odd"
};
println!("{}", result);Solution
Step 1: Check syntax and types
Theifexpression returns string slices "even" or "odd", both same type, so no type mismatch.Step 2: Verify semicolons and operators
The semicolon after theifexpression is present, and the modulo operator is used correctly.Final Answer:
No error, code runs correctly -> Option AQuick Check:
Correct syntax and types = no error [OK]
- Thinking semicolon is missing
- Confusing types in branches
- Misusing modulo operator
if expression to assign a value to a variable based on multiple conditions in Rust?
Choose the correct code snippet.Solution
Step 1: Understand Rust's if-else if syntax
Rust usesif condition { value }andelse if condition { value }blocks with braces, no keywords like then or arrows.Step 2: Check each option for syntax correctness
let grade = if score >= 90 { 'A' } else if score >= 80 { 'B' } else { 'C' }; uses correct Rust syntax; others use invalid keywords or punctuation.Final Answer:
let grade = if score >= 90 { 'A' } else if score >= 80 { 'B' } else { 'C' }; -> Option DQuick Check:
Rust if-else if uses braces, no then [OK]
- Adding 'then' keyword
- Using colons or arrows
- Incorrect else if syntax
Ready to prove it?
Pass the test and this pattern moves unknown
