YouTip LogoYouTip

Rust Conditions

```html\\n

Rust Conditional Statements

\\n\\n

Rust Tutorial

\\n

Rust TutorialRust IntroductionRust Environment SetupCargo TutorialRust Output to Command LineRust Basic SyntaxRust OperatorsRust Data TypesRust CommentsRust FunctionsRust Conditional StatementsRust LoopsRust IteratorsRust ClosuresRust OwnershipRust Slice TypesRust StructsRust EnumsRust Organization and ManagementRust Error HandlingRust Generics and TraitsRust LifetimesRust File and IORust Collections and StringsRust Object-Oriented ProgrammingRust ConcurrencyRust MacrosRust Smart PointersRust Async Programming

\\n

Rust Functions

\\n

Rust Loops

\\n

Rust Conditional Statements

\\n

In Rust language, conditional statements follow this format:

\\n

Example

\\n

fn main(){

\\n

let number =3;

\\n

if number <5{

\\n

println!("Condition is true");

\\n

}else{

\\n

println!("Condition is false");

\\n

}

\\n

}

\\n

In the above program, there is a conditional if statement. This syntax is common in many other languages, but there are some differences: First, the conditional expression number < 5 does not need to be enclosed in parentheses (note: not needed doesn't mean prohibited); However, in Rust, there is no rule that single statements don't need {}, and using a single statement to replace a block is not allowed. Nevertheless, Rust still supports the traditional else-if syntax:

\\n

Example

\\n

fn main(){

\\n

let a

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```

\\n```
← Rust OwnershipRust Comments β†’