Rust: Using if in a let Statement for Concise Conditional Assignments

preview_player
Показать описание
In Rust, you can use the if expression in a let statement to conditionally assign values to a variable. This is useful when you need to make a decision based on a condition and store the result in a variable. Using if in a let statement allows you to create more concise and readable code.

In this code example, we define a variable number and assign it the value 7. Then, we use an if expression in a let statement to determine whether the number is even or odd. If the number is even (number % 2 == 0), the variable message is assigned the value "Even number". Otherwise, message is assigned the value "Odd number". Finally, the program prints the result to the console.
Рекомендации по теме
join shbcf.ru