-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALl type done .rs
More file actions
149 lines (112 loc) · 3.05 KB
/
ALl type done .rs
File metadata and controls
149 lines (112 loc) · 3.05 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
fn main() {
// Scalar Types
// Integer Types
let x: i32 = -10;
let y: u32 = 42;
// Floating-Point Types
let a: f32 = 3.14;
let b: f64 = 2.71828;
// Boolean Type
let is_active: bool = true;
// Character Type
let letter: char = 'A';
let emoji: char = '😊';
// Compound Types
// Tuple
let tup: (i32, f64, char) = (500, 6.4, 'Z');
let (x, y, z) = tup; // Destructuring
// Array
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let first = arr[0];
// User-Defined Types
// Struct
struct Point {
x: f64,
y: f64,
}
let p = Point { x: 1.0, y: 2.0 };
// Enum
enum Direction {
North,
South,
East,
West,
}
let dir = Direction::North;
// Option Type
let some_number: Option<i32> = Some(5);
let absent_number: Option<i32> = None;
// Result Type
fn divide(numerator: f64, denominator: f64) -> Result<f64, String> {
if denominator == 0.0 {
Err(String::from("Division by zero"))
} else {
Ok(numerator / denominator)
}
}
let result = divide(10.0, 2.0);
match result {
Ok(value) => println!("Result: {}", value),
Err(err) => println!("Error: {}", err),
}
// Special Types
// Slice
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..3];
println!("Slice: {:?}", slice);
// Strings
let s: String = String::from("Hello, world!");
let slice: &str = &s[0..5];
println!("String slice: {}", slice);
}
struct LinkedList {
head: Option<Box<ListNode>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn push(&mut self, val: i32) {
let new_node = Box::new(ListNode { val, next: self.head.take() });
self.head = Some(new_node);
}
fn pop(&mut self) -> Option<i32> {
self.head.take().map(|node| {
self.head = node.next;
node.val
})
}
}
// Linked List Types done
use std::collections::LinkedList;
fn main() {
// Create a new empty linked list
let mut list: LinkedList<i32> = LinkedList::new();
// Push elements to the front and back of the list
list.push_back(10);
list.push_back(20);
list.push_front(5);
// Print the linked list
println!("List after pushing elements: {:?}", list);
// Pop elements from the front and back
let front = list.pop_front();
let back = list.pop_back();
println!("Popped front: {:?}", front);
println!("Popped back: {:?}", back);
println!("List after popping: {:?}", list);
// Access the front and back elements without removing them
if let Some(first) = list.front() {
println!("Front element: {}", first);
}
if let Some(last) = list.back() {
println!("Back element: {}", last);
}
// Iterate over the list
println!("Iterating over the list:");
for elem in list.iter() {
println!("{}", elem);
}
// Clear the list
list.clear();
println!("List after clearing: {:?}", list);
}