- Rust Programming By Example
- Guillaume Gomez Antoni Boucher
- 151字
- 2021-07-02 19:12:57
Writing a condition
Similar to other languages, Rust conditions are expressed with the if and else keywords:
let number1 = 24; let number2 = 42; if number1 > number2 { println!("{} > {}", number1, number2); } else { println!("{} <= {}", number1, number2); }
However, they do not require parentheses around the conditional expression. Also, this expression must be of the bool type: you cannot use a number as you would in other languages.
One particularity of Rust conditions, like many other constructs, is that they are expressions. The last expression of each branch is the value of this branch. Be careful though, the type of each branch must be the same. For instance, we can get the minimum number of the two numbers and put it into a variable:
let minimum = if number1 < number2 { number1 } else { number2 }; // Don't forget the semi-colon here.
推薦閱讀
- 地方檔案與文獻(xiàn)研究(第2輯)
- 國外圖書館動(dòng)漫資源建設(shè)與服務(wù)
- 博物館新視覺:博物館管理叢書
- 山東圖書館學(xué)史研究
- 知中8·了不起的宋版書
- 中國人民大學(xué)“復(fù)印報(bào)刊資料”轉(zhuǎn)載指數(shù)排名研究報(bào)告(2014)
- 醫(yī)藥信息處理與分析
- 檔案利用與服務(wù)
- 西夏檔案及其管理制度研究
- 高校圖書館創(chuàng)新服務(wù)(谷臻小簡(jiǎn)·AI導(dǎo)讀版)
- 信息檢索與案例研究
- 讓文明得到更好的傳承:公共文化服務(wù)體系中公共檔案館發(fā)展戰(zhàn)略
- 檔案與少數(shù)民族記憶
- 高校圖書館學(xué)科服務(wù)研究
- 中國富布賴特學(xué)友會(huì)(籌)論文集(2013)