Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Rust If .. Else Conditions


Conditions and If..Else

You have already learned that Rust supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

Rust has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Note: Unlike many other programming languages, if..else can be used as a statement or as an expression (to assign a value to a variable) in Rust. See an example at the bottom of the page to better understand it.


if

Use if to specify a block of code to be executed if a condition is true.

Example

if 7 > 5 {
  println!("7 is greater than 5.");
}
Try it Yourself »

You can also test variables:

Example

let x = 7;
let y = 5;

if x > y {
  println!("x is greater than y.");
}
Try it Yourself »

if...else

If the condition is not true, you can use else to run different code:

Example

let age = 16;

if age >= 18 {
  println!("You can vote.");
} else {
  println!("You are too young to vote.");
}
Try it Yourself »

else if

You can check multiple conditions using else if:

Example

let score = 85;

if score >= 90 {
  println!("Grade: A");
} else if score >= 80 {
  println!("Grade: B");
} else if score >= 70 {
  println!("Grade: C");
} else {
  println!("Grade: F");
}
Try it Yourself »

Using if as an Expression

In Rust, if...else can also be used as an expression.

This means you can assign the result of an if to a variable:

Example

let time = 20;
let greeting = if time < 18 {
  "Good day."
} else {
  "Good evening."
};
println!("{}", greeting);
Try it Yourself »

When using if as an expression, you must include else . This ensures the result always has a value.


Simplified Syntax

If each block only contains one line, you can remove the curly braces {} and write it in a shorter way:

Example

let time = 20;
let greeting = if time < 18 { "Good day." } else { "Good evening." };
println!("{}", greeting);
Try it Yourself »

Note: The value from if and else must be the same type, like two pieces of text or two numbers (in our example, both are strings).

Tip: This example works similarly to the ternary operator (short-hand if...else) in languages like Java or C.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.