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

Rust Nested If


Share

Nested If

You can put an if statement inside another if statement. This is called a nested if.

It is useful when a second question only matters if the answer to the first one is yes. For example, a concert only checks your ticket if you are old enough to enter:

Example

let age = 20;
let has_ticket = true;

if age >= 18 {
  println!("You are old enough.");
  if has_ticket {
    println!("Welcome to the concert!");
  } else {
    println!("You need a ticket.");
  }
} else {
  println!("You are too young.");
}
Try it Yourself »

Example explained

  • The outer if checks the age. 20 >= 18 is true, so it prints "You are old enough."
  • Then the inner if checks the ticket. has_ticket is true, so it prints "Welcome to the concert!"
  • If age was 16, the inner if would never run. The program would only print "You are too young."


Use && Instead

When you only need one answer, you can often replace a nested if with the && (AND) operator from the Operators chapter. The condition is only true if both parts are true:

Example

let age = 20;
let has_ticket = true;

if age >= 18 && has_ticket {
  println!("Welcome to the concert!");
} else {
  println!("You cannot enter.");
}
Try it Yourself »

This is shorter, but the program can no longer tell the visitor why they cannot enter. Use a nested if when each step needs its own message.


Use || for Either

The || (OR) operator is true if at least one of the conditions is true:

Example

let day = "Saturday";

if day == "Saturday" || day == "Sunday" {
  println!("It is the weekend!");
} else {
  println!("It is a weekday.");
}
Try it Yourself »

Use ! for Not

The ! (NOT) operator turns true into false, and false into true:

Example

let is_raining = false;

if !is_raining {
  println!("Let's go for a walk.");
}
Try it Yourself »

Tip: Try not to nest too deep. Code with many levels of if inside if is hard to read. Often, &&, || or else if can make it flatter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

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, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.