|
| 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 | +## If Statements |
| 80 | + |
| 81 | +In this exercise you must conditionally execute logic. The most common way to do this in Rust is by using an `if/else` statement: |
| 82 | + |
| 83 | +`if` expressions start with the keyword `if`, followed by a condition. In this |
| 84 | +case, the condition checks whether or not the variable `number` has a value less |
| 85 | +than `5`. |
| 86 | + |
| 87 | + |
| 88 | +```rust |
| 89 | +let number = 3; |
| 90 | + |
| 91 | +if number < 5 { |
| 92 | + println!("condition was true"); |
| 93 | +} else { |
| 94 | + println!("condition was false"); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +Optionally, we can also include an `else` expression, which we chose to do here, |
| 99 | +to give the program an alternative block of code to execute should the condition |
| 100 | +evaluate to `false`. |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +The condition of an `if` statement must be of type `bool`. Rust has no concept of |
| 105 | +_truthy_ values. |
| 106 | + |
| 107 | +```rust |
| 108 | +let number = 3; |
| 109 | + |
| 110 | +if number { |
| 111 | + println!("number was three"); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +The `if` condition evaluates to a value of `3` this time, and Rust throws an |
| 116 | +error: |
| 117 | + |
| 118 | +```txt |
| 119 | + | |
| 120 | +4 | if number { |
| 121 | + | ^^^^^^ expected `bool`, found integer |
| 122 | +
|
| 123 | +For more information about this error, try `rustc --explain E0308`. |
| 124 | +error: could not compile `branches` due to previous error |
| 125 | +``` |
| 126 | + |
| 127 | +You can use multiple conditions by combining `if` and `else` in an `else if` expression. For example: |
| 128 | + |
| 129 | +```rust |
| 130 | +let x = 6; |
| 131 | + |
| 132 | +if x == 5 { |
| 133 | + // Execute logic if x equals 5 |
| 134 | +} else if x > 7 { |
| 135 | + // Execute logic if x greater than 7 |
| 136 | +} else { |
| 137 | + // Execute logic in all other cases |
| 138 | +} |
| 139 | +``` |
0 commit comments