-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.rs
More file actions
100 lines (84 loc) · 1.88 KB
/
example.rs
File metadata and controls
100 lines (84 loc) · 1.88 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Type aliases
type Text = String;
type IntVector = Vec<i32>;
// Simple enum
enum Colour {
Red,
Green,
Blue,
}
// Struct with nested struct
struct Outer {
value: i32,
inner: InnerStruct,
}
struct InnerStruct {
name: String,
code: i32,
}
// Struct with nested enum
struct Shape {
centre_x: f64,
centre_y: f64,
shape_type: ShapeType,
}
enum ShapeType {
Circle,
Square,
Triangle,
}
// Generic struct (template)
struct Box<T> {
content: T,
}
// Struct with nested function via field (function pointer)
struct Wrapper {
value: i32,
get_multiplier: fn(i32, i32) -> i32,
}
// Function with nested function and local struct
fn function_with_local_class() {
// Local struct
struct LocalStruct {
x: i32,
}
// Local enum
enum LocalEnum {
One,
Two,
}
// Local type alias
type LocalAlias = i32;
// Nested function (Rust allows this!)
fn nested_function(a: i32, b: i32) -> i32 {
// Triple nested function
fn inner_helper(x: i32, y: i32) -> i32 {
x + y
}
inner_helper(a, b)
}
// Closure (also nested)
let closure = |x: i32| -> i32 {
let inner = |a: i32, b: i32| a * b;
inner(x, 2)
};
let _ = closure(5);
}
// Function returning closure (nested function)
fn wrapper_get_multiplier(value: i32, factor: i32) -> impl Fn(i32) -> i32 {
move |x: i32| {
// Nested function inside closure
fn inner_helper(a: i32, b: i32) -> i32 {
a * b
}
inner_helper(value + x, factor)
}
}
// Variable definitions
static ORIGIN: (i32, i32) = (0, 0);
static mut BG_COLOUR: Colour = Colour::Blue;
static INT_BOX: Box<i32> = Box { content: 42 };
static NUMBERS: IntVector = vec![1, 2, 3];
fn main() {
// Variables can be declared here, but no statements per request
}