-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
348 lines (327 loc) · 11 KB
/
index.html
File metadata and controls
348 lines (327 loc) · 11 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rust vs. Java Code Comparison</title>
<link
href="https://fonts.googleapis.com/css2?family=Roboto"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css"
/>
<style>
:root {
--primary-color: #ff79c6;
--secondary-color: #bd93f9;
}
body {
font-family: "Roboto", sans-serif;
background-image: linear-gradient(
to right,
var(--primary-color) 0%,
var(--secondary-color) 100%
);
}
h1,
h2 {
color: #fff;
}
pre {
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
}
#summary {
background-color: rgba(0, 0, 0, 0.4);
color: #fff;
}
.message {
display: none;
}
/* show a friendly message for non-desktop screens */
@media only screen and (max-width: 767px) {
/* blur the container */
.container {
filter: blur(5px);
}
.message {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
text-align: center;
font-size: 18px;
font-weight: bold;
padding: 30px;
z-index: 1;
}
.message p {
position: relative;
display: inline-block;
color: #fff;
text-shadow: 0 0 10px #fff;
}
/* hide the nav */
nav {
display: none !important;
}
}
</style>
</head>
<body>
<div class="message">
<p>
This content is not optimized for small screens. Please view this on a
desktop for the best experience.
</p>
</div>
<div class="container mx-auto p-4 py-9">
<h1 class="text-4xl text-center font-medium mb-4" id="header"></h1>
<div class="grid md:grid-cols-2 sm:grid-cols-1">
<div class="col-span-1 pr-4">
<h2 class="text-2xl font-medium">Rust</h2>
<pre
class="p-4 rounded-lg overflow-auto"
><code id="rust-code"></code></pre>
</div>
<div class="col-span-1">
<h2 class="text-2xl font-medium">Java</h2>
<pre
class="p-4 rounded-lg overflow-auto"
><code id="java-code"></code></pre>
</div>
</div>
<div class="mt-4">
<h2 class="text-2xl font-medium">Summary</h2>
<p class="p-4 rounded-lg" id="summary"></p>
</div>
</div>
<nav
class="fixed bottom-0 left-0 right-0 bg-gray-800 p-4 flex justify-center"
>
<!-- Nav buttons will be added here -->
</nav>
<script>
let codeSnippets = {
page1: {
header: "Hello World Example",
rust: `fn main() {
println!("Hello, world!");
}`,
java: `public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}`,
summary: `Rust and Java are both object-oriented programming languages. They both have a main function that is the entry point of the program. In Rust, the main function is declared with the fn keyword. In Java, the main function is declared with the public static void keywords.`,
},
page2: {
header: "Sum Function Example",
rust: `fn sum(a: i32, b: i32) -> i32 {
a + b
}`,
java: `public int sum(int a, int b) {
return a + b;
}`,
summary: `Rust and Java are both statically typed languages. In Rust, the type of a variable is declared after the variable name. In Java, the type of a variable is declared before the variable name. In Rust, the type of a function parameter is declared after the parameter name. In Java, the type of a function parameter is declared before the parameter name. In Rust, the return type of a function is declared after the function parameters. In Java, the return type of a function is declared after the function name.`,
},
page3: {
header: "Variable Declarations",
rust: `fn main() {
let x = 5;
let mut y = 10;
}`,
java: `public class Main {
public static void main(String[] args) {
int x = 5;
int y = 10;
}`,
summary: `In Rust, variables are immutable by default. To make a variable mutable, the mut keyword is used. In Java, variables are mutable by default. To make a variable immutable, the final keyword is used.`,
},
page4: {
header: "String Concatenation",
rust: `fn main() {
let name = "John";
let greeting = "Hello, ".to_string() + name;
}`,
java: `public class Main {
public static void main(String[] args) {
String name = "John";
String greeting = "Hello, " + name;
}
}`,
summary: `In Rust, the + operator is used to concatenate strings. The to_string() method is used to convert a string literal to a String object. The object is allocated on the heap.
In Java, the + operator is used to concatenate strings. Strings are allocated on the heap.`,
},
page5: {
header: "String Interpolation",
rust: `fn main() {
let name = "John";
let greeting = format!("Hello, {}", name);
}`,
java: `public class Main {
public static void main(String[] args) {
String name = "John";
String greeting = "Hello, " + name;
}
}`,
summary: `In Rust, the format! macro is used to interpolate strings. Strings are allocated on the stack. The macro is used to avoid allocating memory on the heap.
In Java, the + operator is used to concatenate strings. Strings are allocated on the heap.`,
},
page6: {
header: "Arrays",
rust: `fn main() {
let numbers = [1, 2, 3];
for number in numbers.iter() {
println!("{}", number);
}
}`,
java: `public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
for (int number : numbers) {
System.out.println(number);
}
}
}`,
summary: `In Rust, arrays are declared with the [type; size] syntax. They are iterated with the for loop. Arrays allocate memory on the stack.
In Java, arrays are declared with the type[] syntax. They are iterated with the for-each loop. Arrays allocate memory on the heap.`,
},
page7: {
header: "Vectors",
rust: `fn main() {
let numbers = vec![1, 2, 3];
for number in numbers {
println!("{}", number);
}
}`,
java: `public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (int number : numbers) {
System.out.println(number);
}
}
}`,
summary: `In Rust, vectors are declared with the vec! macro. They are iterated with the for loop.
In Java, there is no vector type. Instead, ArrayLists are used. They are iterated with the for-each loop.`,
},
page8: {
header: "HashMaps",
rust: `fn main() {
let mut map = HashMap::new();
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);
for (key, value) in map {
println!("{}: {}", key, value);
}
}`,
java: `public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}`,
summary: `In Rust, HashMaps are declared with the HashMap::new() syntax. They are iterated with the for loop.
In Java, HashMaps are declared with the HashMap class. They are iterated with the for-each loop.`,
},
page9: {
header: "Function Declaration",
rust: `fn main() {
println!("{}", add(1, 2));
}
fn add(x: i32, y: i32) -> i32 {
x + y
}`,
java: `public class Main {
public static void main(String[] args) {
System.out.println(add(1, 2));
}
public static int add(int x, int y) {
return x + y;
}
}`,
summary: `In Rust, functions are declared with the fn keyword.
In Java, functions are declared with the static keyword.`,
},
};
function updateCode() {
let page = window.location.hash.substr(1); // get the current page from the URL hash
if (!page) {
page = "page1";
}
if (codeSnippets[page]) {
document.getElementById("header").innerText =
codeSnippets[page].header;
document.getElementById("rust-code").innerText =
codeSnippets[page].rust;
document.getElementById("java-code").innerText =
codeSnippets[page].java;
document.getElementById("summary").innerText =
codeSnippets[page].summary;
}
}
function updateNav() {
let page = window.location.hash.substr(1);
let links = document.querySelectorAll("nav a");
links.forEach((link) => {
console.log(link.getAttribute("href"));
console.log(link.getAttribute("href") === "#" + page);
if (link.getAttribute("href") === "#" + page) {
link.classList.add("text-blue-500");
} else {
link.classList.remove("text-blue-500");
}
});
}
function createNavButtons(pages) {
let nav = document.querySelector("nav");
for (let page in pages) {
let button = document.createElement("a");
button.classList.add("text-white", "hover:text-blue-500", "mx-4");
button.setAttribute("href", "#" + page);
button.innerText = pages[page].header;
nav.appendChild(button);
}
}
function switchPage(direction) {
let currentPage = window.location.hash.substr(1);
let pages = Object.keys(codeSnippets);
let currentIndex = pages.indexOf(currentPage);
if (direction === "next" && currentIndex < pages.length - 1) {
window.location.hash = pages[currentIndex + 1];
} else if (direction === "prev" && currentIndex > 0) {
window.location.hash = pages[currentIndex - 1];
}
updateCode();
updateNav();
}
createNavButtons(codeSnippets);
updateCode();
updateNav();
window.addEventListener("hashchange", function () {
updateCode();
updateNav();
});
window.addEventListener("keydown", function (event) {
if (event.key === "ArrowRight") {
switchPage("next");
} else if (event.key === "ArrowLeft") {
switchPage("prev");
}
});
</script>
</body>
</html>