Complete the code to assign the correct value using if as an expression.
let number = 5; let result = if number > 0 [1] "positive" else "non-positive"; println!("{}", result);
The if expression must be followed by a block {} when it has multiple statements or to clearly define the expression. Here, { starts the block.
Complete the code to assign the correct string based on the value of score using if as an expression.
let score = 85; let grade = if score >= 90 [1] "A" else "B"; println!("Grade: {}", grade);
The if expression must be followed by a block {} to return a value. Here, { starts the block for the true branch.
Fix the error in the code by completing the if expression correctly.
let x = 10; let y = if x < 5 [1] 1 else 2; println!("{}", y);
The if expression must have a block {} to return a value. Here, { starts the block for the true branch.
Complete the code to create a dictionary comprehension that uses if as an expression.
let words = ["apple", "bee", "cat"]; let lengths = words.iter().map(|word| (word, word.len() { 3 {BLANK_2}} "long" else "short")).collect::<Vec<_>>();
The if expression needs a block {} and a comparison operator > to check if the length is greater than 3.
Fill all three blanks to assign a value using nested if expressions.
let num = 7; let description = if num [1] 0 [2] "positive" else if num [3] 0 {"negative"} else {"zero"};
The first condition checks if num is greater than 0, the else if checks if it is greater or equal to 0, and the last else covers the negative case with less than 0.