-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata_types.rs
76 lines (49 loc) · 1.92 KB
/
data_types.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
fn main() {
println!("Hello, Rust Data types!");
// Note: I put underscores before `x` because the Rust compiler gave a bunch of "unused variable" warnings.
//////////////////////// PRIMITIVE TYPES ////////////////////////
// i32 (signed 32-bit integer) (default integer type)
let _x: i32 = 2;
let _x = 2;
// i8 (signed 8-bit integer)
let _x: i8 = 2;
// i16 (signed 16-bit integer)
let _x: i16 = 2;
// i64 (signed 64-bit integer)
let _x: i64 = 2;
// i128 (signed 128-bit integer)
let _x: i128 = 2;
///////////////////////////////////////////////////////////////////
// uint (same as signed int, but can't be negative)
// u32 (unsigned 32-bit integer)
let _x: u32 = 2;
// u8 (unsigned 8-bit integer)
let _x: u8 = 2;
// u16 (unsigned 16-bit integer)
let _x: u16 = 2;
// u64 (unsigned 64-bit integer)
let _x: u64 = 2;
// u128 (unsigned 128-bit integer)
let _x: u128 = 2;
///////////////////////////////////////////////////////////////////
// Floating point value (f32 (single precision, 32-bit value), f64 (double precision, 64-bit value, default floating point type))
let _x: f32 = 10.9;
let _x: f64 = 10.9;
///////////////////////////////////////////////////////////////////
// Booleans (True = 1, False = 0)
let _x: bool = true;
let _x: bool = false;
let _x = 1;
let _x = 0;
///////////////////////////////////////////////////////////////////
// Characters (type char, stores one character, uses single quotes)
let _x: char = 'd';
//////////////////////// COMPOUND TYPES ////////////////////////
// Tuples (immutable by default)
let x: (i32, bool, char) = (1, true, 's');
println!("{}", x.0);
// Arrays (immutable by default) (type doesn't have to be defined explicitly)
let mut x: [i32; 5] = [1, 2, 3, 4, 5];
x[2] = x[2] * 2;
println!("{}", x[2]);
}