|
| 1 | +# Introduction |
| 2 | + |
| 3 | +## Numbers |
| 4 | + |
| 5 | +There are two different types of numbers in Rust: |
| 6 | + |
| 7 | +- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `500000`. |
| 8 | +- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`. |
| 9 | + |
| 10 | +The two default numeric types in Rust are `i32` and `f64`. An `i32` is a 32-bit integer and a `f64` is a 64-bit floating-point number. |
| 11 | + |
| 12 | +Arithmetic is done using the standard arithmetic operators. Numbers can be |
| 13 | +compared using the standard numeric comparison operators and the equality (`==`) |
| 14 | +and inequality (`!=`) operators. |
| 15 | + |
| 16 | +## Assignment |
| 17 | + |
| 18 | +The following syntax can be used to define a variable and assign a value to it. |
| 19 | + |
| 20 | +```rust |
| 21 | +let my_variable = 5; |
| 22 | +``` |
| 23 | + |
| 24 | +The above defines a variable with value 5 which automatically makes its type as |
| 25 | +`i32`. The value of this variable cannot be changed. |
| 26 | +In Rust, `snake_case` is used to define the name of a variable. |
| 27 | + |
| 28 | +To create a variable that can be assigned a different value one can use `mut` |
| 29 | +keyword: |
| 30 | + |
| 31 | +```rust |
| 32 | +let mut my_variable = 5; |
| 33 | +my_variable = 10; |
| 34 | +``` |
| 35 | + |
| 36 | +In Rust, integers and floats are different types hence you cannot assign a |
| 37 | +float to an integer variable and vice-versa. Not even if the values seem equal: |
| 38 | + |
| 39 | +```rust |
| 40 | +let my_int = 5; |
| 41 | +let my_float = my_int; // => panic |
| 42 | + |
| 43 | +let my_float2 = 5.0; |
| 44 | +let my_int2 = my_float2; // => panic |
| 45 | +``` |
| 46 | + |
| 47 | +### Constants |
| 48 | +Like immutable variables, *constants* are values that are bound to a name and |
| 49 | +are not allowed to change, but there are a few differences between constants |
| 50 | +and variables. |
| 51 | + |
| 52 | +- First, you aren’t allowed to use `mut` with constants. |
| 53 | +- Constants aren’t just immutable by default—they’re always immutable. |
| 54 | +- You declare constants using the `const` keyword instead of the `let` keyword, and the type of the value *must* be annotated. |
| 55 | +- Constants can be declared in any scope, including the global scope, which makes them useful for values that many parts of code need to know about. |
| 56 | +- The last difference is that constants may be set only to a constant expression, not the result of a value that could only be computed at runtime. |
| 57 | + |
| 58 | +Here’s an example of a constant declaration: |
| 59 | + |
| 60 | +```rust |
| 61 | +const SECONDS_IN_A_MINUTE: u32 = 60; |
| 62 | +``` |
| 63 | + |
| 64 | +In Rust, constants are defined using `CAPITAL_SNAKE_CASE`. |
| 65 | + |
| 66 | +### Rust requires explicit conversion between types |
| 67 | + |
| 68 | +Using the `.into()` method: |
| 69 | + |
| 70 | +```rust |
| 71 | +let my_int = 50; |
| 72 | +let my_float: f64 = my_int.into(); // works as expected |
| 73 | +``` |
| 74 | + |
| 75 | +Note that this requires specifying the variable type explicitly. |
| 76 | + |
| 77 | +As an `i32` has less precision than a `f64`, converting from an `i32` to a `f64` is safe and lossless. However, converting from a `f64` to an `i32` could mean losing data. |
| 78 | + |
| 79 | +## Numeric Operators |
| 80 | + |
| 81 | +Rust supports various operators on integers and floats. |
| 82 | + |
| 83 | +Take a look at the following table that illustrates how each operator behaves with integers and floats. |
| 84 | + |
| 85 | +| Symbol | Integer | Floating Point | |
| 86 | +|--------|-------------------------|----------------| |
| 87 | +| `+` | Addition | Addition | |
| 88 | +| `-` | Subtraction | Subtraction | |
| 89 | +| `*` | Multiplication | Multiplication | |
| 90 | +| `/` | Division* | Division | |
| 91 | +| `%` | Remainder** | Remainder | |
| 92 | + |
| 93 | +\* Integer division rounds towards zero. |
| 94 | + |
| 95 | +\*\* Rust uses a remainder defined with [truncating division]. |
| 96 | +Given `remainder = dividend % divisor`, the remainder will have the same sign as the dividend. |
| 97 | + |
| 98 | +Note that both operands of an operator must be of same type. |
| 99 | +e.g. For `+` operator, an integer cannot be added to a float and vice-versa. |
| 100 | + |
| 101 | +Following example demonstrates usage for these operators. |
| 102 | + |
| 103 | +```rust |
| 104 | +let result_1 = 3 + 6; |
| 105 | +// => 9 |
| 106 | +let result_2 = 5.5 - 1.25; |
| 107 | +// => 4.25 |
| 108 | +let result_3 = -5 * 14; |
| 109 | +// => -70 |
| 110 | +let result_4 = 14 / 3; |
| 111 | +// => 4 |
| 112 | +let result_5 = 100 % 7; |
| 113 | +// => 2 |
| 114 | +``` |
| 115 | + |
| 116 | +## If Statements |
| 117 | + |
| 118 | +In this exercise you must conditionally execute logic. The most common way to do this in Rust is by using an `if/else` statement: |
| 119 | + |
| 120 | +`if` expressions start with the keyword `if`, followed by a condition. In this |
| 121 | +case, the condition checks whether or not the variable `number` has a value less |
| 122 | +than `5`. |
| 123 | + |
| 124 | + |
| 125 | +```rust |
| 126 | +let number = 3; |
| 127 | + |
| 128 | +if number < 5 { |
| 129 | + println!("condition was true"); |
| 130 | +} else { |
| 131 | + println!("condition was false"); |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +Optionally, we can also include an `else` expression, which we chose to do here, |
| 136 | +to give the program an alternative block of code to execute should the condition |
| 137 | +evaluate to `false`. |
| 138 | + |
| 139 | +If you don’t provide an `else` expression and the condition is `false`, the program will just skip the `if` block and move on to the next bit of code. |
| 140 | + |
| 141 | +The condition of an `if` statement must be of type `bool`. Rust has no concept of |
| 142 | +_truthy_ values. |
| 143 | + |
| 144 | +```rust |
| 145 | +let number = 3; |
| 146 | + |
| 147 | +if number { |
| 148 | + println!("number was three"); |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +The `if` condition evaluates to a value of `3` this time, and Rust throws an |
| 153 | +error: |
| 154 | + |
| 155 | +```txt |
| 156 | + | |
| 157 | +4 | if number { |
| 158 | + | ^^^^^^ expected `bool`, found integer |
| 159 | +
|
| 160 | +For more information about this error, try `rustc --explain E0308`. |
| 161 | +error: could not compile `branches` due to previous error |
| 162 | +``` |
| 163 | + |
| 164 | +You can use multiple conditions by combining `if` and `else` in an `else if` expression. For example: |
| 165 | + |
| 166 | +```rust |
| 167 | +let x = 6; |
| 168 | + |
| 169 | +if x == 5 { |
| 170 | + // Execute logic if x equals 5 |
| 171 | +} else if x > 7 { |
| 172 | + // Execute logic if x greater than 7 |
| 173 | +} else { |
| 174 | + // Execute logic in all other cases |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | + |
| 179 | +[truncating division]: |
| 180 | + https://en.wikipedia.org/wiki/Modulo_operation#Variants_of_the_definition |
0 commit comments