- Rust Quick Start Guide
- Daniel Arbuckle
- 449字
- 2021-06-10 19:46:04
Operator expressions
Again, like math, Rust has a number of symbolic operators that can be applied to values to transform them into some new value. For example, + is a Rust operator that adds two values together. So, 2 + 2 is a Rust expression adding the number 2 to itself, producing the number 4. Rust also uses - as the subtraction operator, * as the multiplication operator, / as the division operator, and % as the remainder operator.
Rust is not limited to mathematical operators, though. In Rust, & means and, | means or, ^ means exclusive or, ! means not (!true is false, for example), << means leftward bit shift, and >> means rightward bit shift. Sometimes the meanings of those operations depends on the type of value that they're acting on. For example, | means bitwise or when applied to integers, but logical or when applied to Boolean values.
Then there are the comparison operators. The == operator means check whether two values are equal. An expression built around the == operator produces the Boolean value true if the two values being compared are equal, and false if they are not. So, for example, 5 == 4 is an expression producing false as its result. Similarly, != means not equal, > means greater than, < means less than, >= means greater than or equal, and <= means less than or equal. All of them produce true when the relationship is correct, and false when it is not.
Finally, Rust recognizes && and || operators. These can only be applied to Boolean (true or false) values, and produce the same results as & and | do when applied to the same values. The difference is that && and || are what is called lazy or short-circuit operators, which means that they will not bother evaluating their right-side operand if the left-side operand provides enough information to determine the operator's produced value. For example, for the expression false && some_expensive_calculation(), Rust will never bother to run the some_expensive_calculation function, because no matter what the function produced as its result, the result of the && operation is going to be false.
In most situations where we'd use & or | on Boolean values, we should use && or || instead, since it allows Rust to be a little more efficient, especially if we're mindful enough to put the more expensive operations on the right side of the operator.
These are not a full list of Rust's operators, and we'll see some of the more specialized ones as we move onward through the language. These are the operators we need for expressing the majority of calculations, computations, and decisions in our programs, though.
- PyTorch自動駕駛視覺感知算法實戰(zhàn)
- AIRAndroid應(yīng)用開發(fā)實戰(zhàn)
- 神經(jīng)網(wǎng)絡(luò)編程實戰(zhàn):Java語言實現(xiàn)(原書第2版)
- 零基礎(chǔ)學(xué)MQL:基于EA的自動化交易編程
- Java編程的邏輯
- 用戶體驗可視化指南
- Advanced Express Web Application Development
- Flowable流程引擎實戰(zhàn)
- HTML5權(quán)威指南
- JSP程序設(shè)計與案例實戰(zhàn)(慕課版)
- Go語言入門經(jīng)典
- 零基礎(chǔ)學(xué)C++(升級版)
- 從零開始學(xué)UI:概念解析、實戰(zhàn)提高、突破規(guī)則
- Python機(jī)器學(xué)習(xí)開發(fā)實戰(zhàn)
- Selenium WebDriver Practical Guide