|
| 1 | +const MAX_POINTS: u32 = 100_000; // Can add '_' to num literals for readability |
| 2 | + |
| 3 | + |
| 4 | +fn main() { |
| 5 | + println!("\n=== Using the Constant ==="); |
| 6 | + println!("Max points = {}", MAX_POINTS); |
| 7 | + |
| 8 | + println!("\n=== Mutability ==="); |
| 9 | + mutability(); |
| 10 | + |
| 11 | + println!("\n=== Shadowing ==="); |
| 12 | + shadowing(); |
| 13 | + |
| 14 | + println!("\n=== Mutating Types (illegal) ==="); |
| 15 | + mutate_type(); |
| 16 | + |
| 17 | + println!("\n=== Data Types (see source code) ==="); |
| 18 | + data_types(); |
| 19 | + compound_data_types(); |
| 20 | + numeric_operations(); |
| 21 | +} |
| 22 | + |
| 23 | +fn mutability() { |
| 24 | + let mut x = 5; // mut needed to make it mutable |
| 25 | + println!("The value of x is: {}", x); |
| 26 | + x = 6; |
| 27 | + println!("The value of x is now: {}", x); |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +fn shadowing() { |
| 32 | + let x = 5; |
| 33 | + |
| 34 | + let x = x + 1; |
| 35 | + |
| 36 | + let x = x * 2; |
| 37 | + |
| 38 | + println!("The value of x is: {}", x); |
| 39 | +} |
| 40 | + |
| 41 | +fn mutate_type() { |
| 42 | + let spaces = " "; |
| 43 | + println!("spaces = \"{}\"", spaces); |
| 44 | + let spaces = spaces.len(); |
| 45 | + println!("spaces = \"{}\" \ |
| 46 | + (shadowed - can't actually change type of mutated variable)", |
| 47 | + spaces); |
| 48 | + |
| 49 | + // This is fine because we are just shadowing the first with the second |
| 50 | + // However the following is illegal: |
| 51 | + /* |
| 52 | + let mut spaces = " "; |
| 53 | + spaces = spaces.len(); |
| 54 | + */ |
| 55 | +} |
| 56 | + |
| 57 | +#[allow(unused_variables)] // ignore compiler warnings for unused_variables |
| 58 | +fn data_types() { |
| 59 | + // Integer Types |
| 60 | + let signed_8_bit: i8 = 0; |
| 61 | + let unsigned_8_bit: u8 = 0; |
| 62 | + let signed_16_bit: i16 = 0; |
| 63 | + let unsigned_16_bit: u16 = 0; |
| 64 | + let signed_32_bit: i32 = 0; // default |
| 65 | + let unsigned_32_bit: u32 = 0; |
| 66 | + let signed_64_bit: i64 = 0; |
| 67 | + let unsigned_64_bit: u64 = 0; |
| 68 | + let signed_128_bit: i128 = 0; |
| 69 | + let unsigned_128_bit: u128 = 0; |
| 70 | + let signed_32or64_bit: isize = 0; // architecture-dependent |
| 71 | + let unsigned_32or64_bit: usize = 0; // architecture-dependent |
| 72 | + |
| 73 | + // Integer Literals |
| 74 | + let decimal_type = 98_222; // defaults to i32 |
| 75 | + let hex_type = 0xff; // defaults to i32 |
| 76 | + let octal_type = 0o77; // defaults to i32 |
| 77 | + let binary_type = 0b1111_0000; // defaults to i32 |
| 78 | + let byte_type = b'A'; // defaults to u8 (only) |
| 79 | + let non_byte_type_with_suffix = 57u8; // suffix forces type |
| 80 | + |
| 81 | + // Floating Point Types |
| 82 | + let fp_64 = 2.0; // f64 (default) |
| 83 | + let fp_32: f32 = 3.0; // f32 |
| 84 | + |
| 85 | + // Boolean Types |
| 86 | + let t = true; |
| 87 | + let f: bool = false; // with explicit type annotation (not needed) |
| 88 | + |
| 89 | + // Character types |
| 90 | + let char_lower = 'z'; |
| 91 | + let char_upper = 'Z'; |
| 92 | + let emoji = '😻'; // Note unicode scalar, so more than just ASCII! |
| 93 | + // Range: U+0000 to U+D7FF and U+E000 to U+10FFFF inclusive |
| 94 | +} |
| 95 | + |
| 96 | +#[allow(unused_variables)] // ignore compiler warnings for unused_variables |
| 97 | +fn compound_data_types() { |
| 98 | + // Tuple Type |
| 99 | + let tup_explicit: (i32, f64, u8) = (500, 6.4, 1); |
| 100 | + let tup_implicit = (500, 6.4, 1); |
| 101 | + |
| 102 | + // now to get an individual value (destructuring): |
| 103 | + let (x, y, z) = tup_implicit; // now x = 500, y = 6.4, z = 1 |
| 104 | + |
| 105 | + // Alternatively, use a period (.) |
| 106 | + let five_hundred = tup_implicit.0; |
| 107 | + let six_point_four = tup_implicit.1; |
| 108 | + let one = tup_implicit.2; |
| 109 | + |
| 110 | + // Array Type |
| 111 | + let arr_explicit: [u8;5] = [1, 2, 3, 4, 5]; // Limited to 5 elements [u8;5] |
| 112 | + let arr_implicit = [1, 2, 3, 4, 5]; // Limited to 5 elements [i32;5] |
| 113 | + let months = ["January", "February", "March", "April", "May", |
| 114 | + "June", "July", "August", "September", "October", "November", |
| 115 | + "December"]; // [&str; 12] |
| 116 | + |
| 117 | + // Array Access |
| 118 | + let first_num = arr_implicit[0]; // i32 |
| 119 | + let second_month = months[1]; // &str |
| 120 | + // let thirteenth_month = months[12]: runtime error! (not compile) |
| 121 | +} |
| 122 | + |
| 123 | +#[allow(unused_variables)] // ignore compiler warnings for unused_variables |
| 124 | +fn numeric_operations() { |
| 125 | + // addition |
| 126 | + let sum = 5 + 10; |
| 127 | + |
| 128 | + // subtraction |
| 129 | + let difference = 95.5 - 4.3; |
| 130 | + |
| 131 | + // multiplication |
| 132 | + let product = 4 * 30; |
| 133 | + |
| 134 | + // division |
| 135 | + let float_quotient = 56.7 / 32.2; // = 1.7608695652173911 |
| 136 | + let int_quotient2 = 5/2; // = 2 |
| 137 | + |
| 138 | + // remainder |
| 139 | + let remainder = 43 % 5; |
| 140 | +} |
0 commit comments