|
| 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 | +Read in-depth about integer and floating point numbers in the [Rust book][rust-book-data-types]. |
| 17 | + |
| 18 | +## Assignment |
| 19 | + |
| 20 | +The following syntax can be used to define a variable and assign a value to it. |
| 21 | +```rust |
| 22 | +let my_variable = 5; |
| 23 | +``` |
| 24 | + |
| 25 | +The above defines a variable with value 5 which automatically makes its type as |
| 26 | +`i32`. The value of this variable cannot be changed. In Rust, `snake_case` is used |
| 27 | +to define the name of a variable |
| 28 | + |
| 29 | + |
| 30 | +To create a variable that can be assigned a different value one can use `mut` |
| 31 | +keyword: |
| 32 | +```rust |
| 33 | +let mut my_variable = 5; |
| 34 | +my_variable = 10; |
| 35 | +``` |
| 36 | + |
| 37 | +In Rust, integers and floats are different types hence you cannot assigne a |
| 38 | +float to and integer variable and vice-versa. Not even if the values seem equal: |
| 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 | + |
| 48 | +### Rust requires explicit conversion between types: |
| 49 | + |
| 50 | +1. The `as` keyword can be used for casting: |
| 51 | + ```rust |
| 52 | + let my_int = 50; // => i32 |
| 53 | + let my_float = my_int as f64; // works as expected |
| 54 | + ``` |
| 55 | +1. Using the `.into()` method: |
| 56 | + ```rust |
| 57 | + let my_int = 50; |
| 58 | + let my_float: f64 = my_int.into(); // works as expected |
| 59 | + ``` |
| 60 | + Note that this requires specifying the variable type explicitly. |
| 61 | + |
| 62 | + |
| 63 | +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. |
| 64 | + |
| 65 | +## If Statements |
| 66 | + |
| 67 | +In this exercise you must conditionally execute logic. The most common way to do this in Rust is by using an `if/else` statement: |
| 68 | + |
| 69 | +```rust |
| 70 | +let x = 6; |
| 71 | + |
| 72 | +if x == 5 { |
| 73 | + // Execute logic if x equals 5 |
| 74 | +} else if x > 7 { |
| 75 | + // Execute logic if x greater than 7 |
| 76 | +} else { |
| 77 | + // Execute logic in all other cases |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +The condition of an `if` statement must be of type `bool`. Rust has no concept of |
| 82 | +_truthy_ values. |
| 83 | + |
| 84 | +[rust-book-data-types]: |
| 85 | + https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types |
0 commit comments