- 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.
- Clojure Programming Cookbook
- 無代碼編程:用云表搭建企業(yè)數字化管理平臺
- C語言最佳實踐
- 營銷數據科學:用R和Python進行預測分析的建模技術
- 網店設計看這本就夠了
- 零基礎學Python網絡爬蟲案例實戰(zhàn)全流程詳解(高級進階篇)
- 軟件測試技術指南
- Python編程從0到1(視頻教學版)
- Python3.5從零開始學
- Vue.js 3.x高效前端開發(fā)(視頻教學版)
- Spring Web Services 2 Cookbook
- 一步一步學Spring Boot:微服務項目實戰(zhàn)(第2版)
- Serverless從入門到進階:架構、原理與實踐
- LiveCode Mobile Development Hotshot
- 零基礎學西門子PLC編程:入門、提高、應用、實例