-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTypes.rs
85 lines (52 loc) · 1.38 KB
/
DataTypes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
let guess: u32 = "42".parse().expect("Not a number!");
// 8bit --> i8 --> u8
// 16bit --> i16 --> u16
// 32bit --> i32 --> u32
// 64bit --> i64 --> u64
// 128bit --> i128 --> u128
// arch --> isize --> usize
// Here’s an example that shows floating-point numbers in action:
fn floatExample() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
}
// Function and return values
fn five() -> i32 {
5
}
// Numeric Operations
fn main() {
// addition
let sum = 5 + 10;
// subtraction
let difference = 95.5 - 4.3;
// multiplication
let product = 4 * 30;
// division
let quotient = 56.7 / 32.2;
let truncated = -5 / 3; // Results in -1
// remainder
let remainder = 43 % 5;
let c = 'z';
let z: char = 'ℤ'; // with explicit type annotation
let heart_eyed_cat = '😻';
// Tuple Type
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
// Array Type
let a = [1, 2, 3, 4, 5];
let b: [i32; 5] = [1, 2, 3, 4, 5];
let c = [3; 5]; // let c = [3, 3, 3, 3, 3];
// Statements and Expressions
let z = {
let d = 3;
d + 1
};
println!("The value of z is: {z}");
// Loops Labels to Disambiguate between multiple loops
}