- Rust Quick Start Guide
- Daniel Arbuckle
- 497字
- 2021-06-10 19:46:05
Branch expressions
One of the things that makes programs truly useful is the ability for them to make decisions. We can do that in Rust by using an if expression. An if expression looks something like this:
if 3 > 4 {
println!("Uh-oh. Three is greater than four.");
}
else if 3 == 4 {
println!("There seems to be something wrong with math.");
}
else {
println!("Three is not greater than or equal to four.");
};
What we have here is an expression that shows off all the features of an if. It starts off with the keyword if, followed by a condition expression that produces either true or false, and then a block expression. If the condition expression produces true, the block expression is run, but if the condition expression produces false, the block expression is not run.
After that, we have an else if and another condition expression and block. That means that, if the first condition expression produced false, the computer should check whether the second one produces true, and if it does, run the associated block expression.
We can chain as many else if expressions as we want after an initial if, so there's no limit to the number of different options we can make available to the computer. However, only one of them will run after any given decision. The computer will start with the initial if and check the values of the conditional expressions one at a time until it finds one that produces true, then it will run the associated block expression, and then it will be done with the if expression and move on to the subsequent instructions.
After an if and any else if we might wish to include, we are allowed to put an else followed by a block expression. This is a branch without a condition, and what it means is if none of the condition expressions produced true, do this. In other words, it allows us to tell the computer what to do by default, if none of the special cases we provided apply.
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- 微服務設計(第2版)
- 案例式C語言程序設計
- Python 3.7網絡爬蟲快速入門
- C#程序設計實訓指導書
- Dependency Injection in .NET Core 2.0
- Reactive Programming With Java 9
- MySQL從入門到精通(軟件開發視頻大講堂)
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- Learning jQuery(Fourth Edition)
- Getting Started with Polymer
- Mastering ASP.NET Web API
- Access 2016數據庫應用與開發:實戰從入門到精通(視頻教學版)
- C#編程魔法書