Greetings, coder! Today, we're delving into Rust to master arithmetic and logical operations. This foundation is fundamental for data manipulation and decision-making within the Rust environment.
Rust's primitive data types include i32 for whole numbers, f32 for decimal numbers, bool for true/false values, and char for single characters.
You can perform arithmetic operations — such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (which represents the remainder of the division, %) — on numerical types. Here's an example:
In many programming languages, including Rust, there's a set of augmented assignment operators which are used as a shorthand method for modifying the value of our variables. These operators are +=, -=, *=, \=, and %=. The += operator adds the value on its right to the variable on its left and assigns the result to the variable. The other operators work similarly. Let's take a look.
Logical operators — && (AND), || (OR), ! (NOT) — evaluate to bool values — true or false within the Rust environment.
&& outputs true only if both boolean values are true, whereas || returns true if either value is true. ! inverses the boolean value.
Below is an example of their application to two bool values:
Rust's logical operations are most commonly applied to variables. Here's a brief example:
