Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- Code_Conversion_Dataset.ipynb +43 -0
- python/api_request.py +7 -0
- python/binary_search_tree.py +32 -0
- python/closures.py +7 -0
- python/concurrency.py +13 -0
- python/error_handling.py +9 -0
- python/factorial.py +7 -0
- python/hello_world.py +1 -0
- python/iterators.py +17 -0
- python/palindrome.py +5 -0
- python/pattern_matching.py +14 -0
- python/person.py +10 -0
- python/quicksort.py +10 -0
- python/web_server.py +17 -0
- python/word_frequency.py +8 -0
- rust/Cargo.toml +11 -0
- rust/factorial.rs +11 -0
- rust/hello_world.rs +3 -0
- rust/palindrome.rs +8 -0
- rust/person.rs +19 -0
- rust/src/api_request.rs +23 -0
- rust/src/binary_search_tree.rs +49 -0
- rust/src/closures.rs +7 -0
- rust/src/concurrency.rs +14 -0
- rust/src/error_handling.rs +19 -0
- rust/src/iterators.rs +31 -0
- rust/src/main.rs +25 -0
- rust/src/pattern_matching.rs +26 -0
- rust/src/quicksort.rs +29 -0
- rust/word_frequency.rs +16 -0
- sample.txt +1 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
Code_Conversion_Dataset.ipynb
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"import pandas as pd\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"def get_code_pairs(directory):\n",
|
| 13 |
+
" pairs = []\n",
|
| 14 |
+
" for filename in os.listdir(os.path.join(directory, \"python\")):\n",
|
| 15 |
+
" if filename.endswith(\".py\"):\n",
|
| 16 |
+
" rust_filename = filename.replace(\".py\", \".rs\")\n",
|
| 17 |
+
" python_path = os.path.join(directory, \"python\", filename)\n",
|
| 18 |
+
" rust_path = os.path.join(directory, \"rust\", rust_filename)\n",
|
| 19 |
+
" if os.path.exists(rust_path):\n",
|
| 20 |
+
" with open(python_path, \"r\") as f:\n",
|
| 21 |
+
" python_code = f.read()\n",
|
| 22 |
+
" with open(rust_path, \"r\") as f:\n",
|
| 23 |
+
" rust_code = f.read()\n",
|
| 24 |
+
" pairs.append((python_code, rust_code))\n",
|
| 25 |
+
" return pairs"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"pairs = get_code_pairs(\".\")\n",
|
| 35 |
+
"df = pd.DataFrame(pairs, columns=[\"Python\", \"Rust\"])\n",
|
| 36 |
+
"df"
|
| 37 |
+
]
|
| 38 |
+
}
|
| 39 |
+
],
|
| 40 |
+
"metadata": {},
|
| 41 |
+
"nbformat": 4,
|
| 42 |
+
"nbformat_minor": 5
|
| 43 |
+
}
|
python/api_request.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This script requires the 'requests' library to be installed.
|
| 2 |
+
# You can install it with: pip install requests
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
response = requests.get('https://jsonplaceholder.typicode.com/todos/1')
|
| 7 |
+
print(response.json())
|
python/binary_search_tree.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Node:
|
| 2 |
+
def __init__(self, key):
|
| 3 |
+
self.left = None
|
| 4 |
+
self.right = None
|
| 5 |
+
self.val = key
|
| 6 |
+
|
| 7 |
+
def insert(root, key):
|
| 8 |
+
if root is None:
|
| 9 |
+
return Node(key)
|
| 10 |
+
else:
|
| 11 |
+
if root.val < key:
|
| 12 |
+
root.right = insert(root.right, key)
|
| 13 |
+
else:
|
| 14 |
+
root.left = insert(root.left, key)
|
| 15 |
+
return root
|
| 16 |
+
|
| 17 |
+
def inorder(root):
|
| 18 |
+
if root:
|
| 19 |
+
inorder(root.left)
|
| 20 |
+
print(root.val)
|
| 21 |
+
inorder(root.right)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
r = Node(50)
|
| 25 |
+
r = insert(r, 30)
|
| 26 |
+
r = insert(r, 20)
|
| 27 |
+
r = insert(r, 40)
|
| 28 |
+
r = insert(r, 70)
|
| 29 |
+
r = insert(r, 60)
|
| 30 |
+
r = insert(r, 80)
|
| 31 |
+
|
| 32 |
+
inorder(r)
|
python/closures.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def outer_func(x):
|
| 2 |
+
def inner_func(y):
|
| 3 |
+
return x + y
|
| 4 |
+
return inner_func
|
| 5 |
+
|
| 6 |
+
add_5 = outer_func(5)
|
| 7 |
+
print(add_5(10))
|
python/concurrency.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
def worker():
|
| 5 |
+
print("Worker thread starting...")
|
| 6 |
+
time.sleep(1)
|
| 7 |
+
print("Worker thread finishing.")
|
| 8 |
+
|
| 9 |
+
thread = threading.Thread(target=worker)
|
| 10 |
+
thread.start()
|
| 11 |
+
thread.join()
|
| 12 |
+
|
| 13 |
+
print("Main thread finishing.")
|
python/error_handling.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def divide(a, b):
|
| 2 |
+
try:
|
| 3 |
+
result = a / b
|
| 4 |
+
print(f"Result: {result}")
|
| 5 |
+
except ZeroDivisionError:
|
| 6 |
+
print("Error: Cannot divide by zero.")
|
| 7 |
+
|
| 8 |
+
divide(10, 2)
|
| 9 |
+
divide(10, 0)
|
python/factorial.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def factorial(n):
|
| 2 |
+
if n == 0:
|
| 3 |
+
return 1
|
| 4 |
+
else:
|
| 5 |
+
return n * factorial(n-1)
|
| 6 |
+
|
| 7 |
+
print(factorial(5))
|
python/hello_world.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
print("Hello, World!")
|
python/iterators.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Fibonacci:
|
| 2 |
+
def __init__(self, limit):
|
| 3 |
+
self.limit = limit
|
| 4 |
+
self.a, self.b = 0, 1
|
| 5 |
+
|
| 6 |
+
def __iter__(self):
|
| 7 |
+
return self
|
| 8 |
+
|
| 9 |
+
def __next__(self):
|
| 10 |
+
if self.a > self.limit:
|
| 11 |
+
raise StopIteration
|
| 12 |
+
val = self.a
|
| 13 |
+
self.a, self.b = self.b, self.a + self.b
|
| 14 |
+
return val
|
| 15 |
+
|
| 16 |
+
for i in Fibonacci(100):
|
| 17 |
+
print(i)
|
python/palindrome.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def is_palindrome(s):
|
| 2 |
+
return s == s[::-1]
|
| 3 |
+
|
| 4 |
+
print(is_palindrome("racecar"))
|
| 5 |
+
print(is_palindrome("hello"))
|
python/pattern_matching.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def describe(x):
|
| 2 |
+
if x == 1:
|
| 3 |
+
print("one")
|
| 4 |
+
elif x == 2:
|
| 5 |
+
print("two")
|
| 6 |
+
elif isinstance(x, str):
|
| 7 |
+
print("a string")
|
| 8 |
+
else:
|
| 9 |
+
print("something else")
|
| 10 |
+
|
| 11 |
+
describe(1)
|
| 12 |
+
describe(2)
|
| 13 |
+
describe("hello")
|
| 14 |
+
describe([1, 2, 3])
|
python/person.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Person:
|
| 2 |
+
def __init__(self, name, age):
|
| 3 |
+
self.name = name
|
| 4 |
+
self.age = age
|
| 5 |
+
|
| 6 |
+
def greet(self):
|
| 7 |
+
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
|
| 8 |
+
|
| 9 |
+
person = Person("Alice", 30)
|
| 10 |
+
person.greet()
|
python/quicksort.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def quicksort(arr):
|
| 2 |
+
if len(arr) <= 1:
|
| 3 |
+
return arr
|
| 4 |
+
pivot = arr[len(arr) // 2]
|
| 5 |
+
left = [x for x in arr if x < pivot]
|
| 6 |
+
middle = [x for x in arr if x == pivot]
|
| 7 |
+
right = [x for x in arr if x > pivot]
|
| 8 |
+
return quicksort(left) + middle + quicksort(right)
|
| 9 |
+
|
| 10 |
+
print(quicksort([3, 6, 8, 10, 1, 2, 1]))
|
python/web_server.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 2 |
+
|
| 3 |
+
class SimpleServer(BaseHTTPRequestHandler):
|
| 4 |
+
def do_GET(self):
|
| 5 |
+
self.send_response(200)
|
| 6 |
+
self.send_header('Content-type', 'text/html')
|
| 7 |
+
self.end_headers()
|
| 8 |
+
self.wfile.write(b'<html><body><h1>Hello, World!</h1></body></html>')
|
| 9 |
+
|
| 10 |
+
def run(server_class=HTTPServer, handler_class=SimpleServer, port=8000):
|
| 11 |
+
server_address = ('', port)
|
| 12 |
+
httpd = server_class(server_address, handler_class)
|
| 13 |
+
print(f'Starting httpd on port {port}...')
|
| 14 |
+
httpd.serve_forever()
|
| 15 |
+
|
| 16 |
+
if __name__ == "__main__":
|
| 17 |
+
run()
|
python/word_frequency.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import Counter
|
| 2 |
+
|
| 3 |
+
def word_frequency(file_path):
|
| 4 |
+
with open(file_path, 'r') as f:
|
| 5 |
+
words = f.read().split()
|
| 6 |
+
return Counter(words)
|
| 7 |
+
|
| 8 |
+
print(word_frequency('sample.txt'))
|
rust/Cargo.toml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[package]
|
| 2 |
+
name = "rust_web_server"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
edition = "2021"
|
| 5 |
+
|
| 6 |
+
[dependencies]
|
| 7 |
+
hyper = { version = "0.14", features = ["full"] }
|
| 8 |
+
tokio = { version = "1", features = ["full"] }
|
| 9 |
+
reqwest = { version = "0.11", features = ["json"] }
|
| 10 |
+
serde = { version = "1.0", features = ["derive"] }
|
| 11 |
+
serde_json = "1.0"
|
rust/factorial.rs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn factorial(n: u64) -> u64 {
|
| 2 |
+
if n == 0 {
|
| 3 |
+
1
|
| 4 |
+
} else {
|
| 5 |
+
n * factorial(n - 1)
|
| 6 |
+
}
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
fn main() {
|
| 10 |
+
println!("{}", factorial(5));
|
| 11 |
+
}
|
rust/hello_world.rs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn main() {
|
| 2 |
+
println!("Hello, World!");
|
| 3 |
+
}
|
rust/palindrome.rs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn is_palindrome(s: &str) -> bool {
|
| 2 |
+
s.chars().eq(s.chars().rev())
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
fn main() {
|
| 6 |
+
println!("{}", is_palindrome("racecar"));
|
| 7 |
+
println!("{}", is_palindrome("hello"));
|
| 8 |
+
}
|
rust/person.rs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
struct Person {
|
| 2 |
+
name: String,
|
| 3 |
+
age: u32,
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
impl Person {
|
| 7 |
+
fn new(name: String, age: u32) -> Person {
|
| 8 |
+
Person { name, age }
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
fn greet(&self) {
|
| 12 |
+
println!("Hello, my name is {} and I am {} years old.", self.name, self.age);
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
fn main() {
|
| 17 |
+
let person = Person::new("Alice".to_string(), 30);
|
| 18 |
+
person.greet();
|
| 19 |
+
}
|
rust/src/api_request.rs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use serde::Deserialize;
|
| 2 |
+
use reqwest;
|
| 3 |
+
|
| 4 |
+
#[derive(Deserialize, Debug)]
|
| 5 |
+
struct Todo {
|
| 6 |
+
#[serde(rename = "userId")]
|
| 7 |
+
user_id: i32,
|
| 8 |
+
id: i32,
|
| 9 |
+
title: String,
|
| 10 |
+
completed: bool,
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
#[tokio::main]
|
| 14 |
+
async fn main() -> Result<(), reqwest::Error> {
|
| 15 |
+
let todo: Todo = reqwest::get("https://jsonplaceholder.typicode.com/todos/1")
|
| 16 |
+
.await?
|
| 17 |
+
.json()
|
| 18 |
+
.await?;
|
| 19 |
+
|
| 20 |
+
println!("{:?}", todo);
|
| 21 |
+
|
| 22 |
+
Ok(())
|
| 23 |
+
}
|
rust/src/binary_search_tree.rs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#![allow(dead_code)]
|
| 2 |
+
|
| 3 |
+
#[derive(Debug)]
|
| 4 |
+
struct Node {
|
| 5 |
+
val: i32,
|
| 6 |
+
left: Option<Box<Node>>,
|
| 7 |
+
right: Option<Box<Node>>,
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
impl Node {
|
| 11 |
+
fn new(val: i32) -> Self {
|
| 12 |
+
Node { val, left: None, right: None }
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
fn insert(root: Option<Box<Node>>, val: i32) -> Option<Box<Node>> {
|
| 17 |
+
match root {
|
| 18 |
+
None => Some(Box::new(Node::new(val))),
|
| 19 |
+
Some(mut node) => {
|
| 20 |
+
if val < node.val {
|
| 21 |
+
node.left = insert(node.left, val);
|
| 22 |
+
} else {
|
| 23 |
+
node.right = insert(node.right, val);
|
| 24 |
+
}
|
| 25 |
+
Some(node)
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
fn inorder(root: &Option<Box<Node>>) {
|
| 31 |
+
if let Some(node) = root {
|
| 32 |
+
inorder(&node.left);
|
| 33 |
+
println!("{}", node.val);
|
| 34 |
+
inorder(&node.right);
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
fn main() {
|
| 39 |
+
let mut root = None;
|
| 40 |
+
root = insert(root, 50);
|
| 41 |
+
root = insert(root, 30);
|
| 42 |
+
root = insert(root, 20);
|
| 43 |
+
root = insert(root, 40);
|
| 44 |
+
root = insert(root, 70);
|
| 45 |
+
root = insert(root, 60);
|
| 46 |
+
root = insert(root, 80);
|
| 47 |
+
|
| 48 |
+
inorder(&root);
|
| 49 |
+
}
|
rust/src/closures.rs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn main() {
|
| 2 |
+
let outer_var = 5;
|
| 3 |
+
let closure = |inner_var| {
|
| 4 |
+
outer_var + inner_var
|
| 5 |
+
};
|
| 6 |
+
println!("{}", closure(10));
|
| 7 |
+
}
|
rust/src/concurrency.rs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use std::thread;
|
| 2 |
+
use std::time::Duration;
|
| 3 |
+
|
| 4 |
+
fn main() {
|
| 5 |
+
let handle = thread::spawn(|| {
|
| 6 |
+
println!("Worker thread starting...");
|
| 7 |
+
thread::sleep(Duration::from_secs(1));
|
| 8 |
+
println!("Worker thread finishing.");
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
handle.join().unwrap();
|
| 12 |
+
|
| 13 |
+
println!("Main thread finishing.");
|
| 14 |
+
}
|
rust/src/error_handling.rs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn divide(a: f64, b: f64) -> Result<f64, String> {
|
| 2 |
+
if b == 0.0 {
|
| 3 |
+
Err("Cannot divide by zero".to_string())
|
| 4 |
+
} else {
|
| 5 |
+
Ok(a / b)
|
| 6 |
+
}
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
fn main() {
|
| 10 |
+
match divide(10.0, 2.0) {
|
| 11 |
+
Ok(result) => println!("Result: {}", result),
|
| 12 |
+
Err(e) => println!("Error: {}", e),
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
match divide(10.0, 0.0) {
|
| 16 |
+
Ok(result) => println!("Result: {}", result),
|
| 17 |
+
Err(e) => println!("Error: {}", e),
|
| 18 |
+
}
|
| 19 |
+
}
|
rust/src/iterators.rs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
struct Fibonacci {
|
| 2 |
+
limit: u32,
|
| 3 |
+
a: u32,
|
| 4 |
+
b: u32,
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
impl Fibonacci {
|
| 8 |
+
fn new(limit: u32) -> Self {
|
| 9 |
+
Fibonacci { limit, a: 0, b: 1 }
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
impl Iterator for Fibonacci {
|
| 14 |
+
type Item = u32;
|
| 15 |
+
|
| 16 |
+
fn next(&mut self) -> Option<Self::Item> {
|
| 17 |
+
if self.a > self.limit {
|
| 18 |
+
return None;
|
| 19 |
+
}
|
| 20 |
+
let val = self.a;
|
| 21 |
+
self.a = self.b;
|
| 22 |
+
self.b = val + self.b;
|
| 23 |
+
Some(val)
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
fn main() {
|
| 28 |
+
for i in Fibonacci::new(100) {
|
| 29 |
+
println!("{}", i);
|
| 30 |
+
}
|
| 31 |
+
}
|
rust/src/main.rs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use hyper::service::{make_service_fn, service_fn};
|
| 2 |
+
use hyper::{Body, Request, Response, Server};
|
| 3 |
+
use std::convert::Infallible;
|
| 4 |
+
use std::net::SocketAddr;
|
| 5 |
+
|
| 6 |
+
async fn handle(_: Request<Body>) -> Result<Response<Body>, Infallible> {
|
| 7 |
+
Ok(Response::new("<h1>Hello, World!</h1>".into()))
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
#[tokio::main]
|
| 11 |
+
async fn main() {
|
| 12 |
+
let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
|
| 13 |
+
|
| 14 |
+
let make_svc = make_service_fn(|_conn| async {
|
| 15 |
+
Ok::<_, Infallible>(service_fn(handle))
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
let server = Server::bind(&addr).serve(make_svc);
|
| 19 |
+
|
| 20 |
+
println!("Listening on http://{}", addr);
|
| 21 |
+
|
| 22 |
+
if let Err(e) = server.await {
|
| 23 |
+
eprintln!("server error: {}", e);
|
| 24 |
+
}
|
| 25 |
+
}
|
rust/src/pattern_matching.rs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn describe(x: &dyn std::any::Any) {
|
| 2 |
+
match x {
|
| 3 |
+
x if x.is::<i32>() => {
|
| 4 |
+
if let Some(val) = x.downcast_ref::<i32>() {
|
| 5 |
+
match val {
|
| 6 |
+
1 => println!("one"),
|
| 7 |
+
2 => println!("two"),
|
| 8 |
+
_ => println!("an integer"),
|
| 9 |
+
}
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
x if x.is::<&str>() => {
|
| 13 |
+
println!("a string");
|
| 14 |
+
}
|
| 15 |
+
_ => {
|
| 16 |
+
println!("something else");
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
fn main() {
|
| 22 |
+
describe(&1);
|
| 23 |
+
describe(&2);
|
| 24 |
+
describe(&"hello");
|
| 25 |
+
describe(&vec![1, 2, 3]);
|
| 26 |
+
}
|
rust/src/quicksort.rs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fn quicksort<T: Ord>(arr: &mut [T]) {
|
| 2 |
+
if arr.len() <= 1 {
|
| 3 |
+
return;
|
| 4 |
+
}
|
| 5 |
+
let pivot = arr.len() / 2;
|
| 6 |
+
let (left, right) = arr.split_at_mut(pivot);
|
| 7 |
+
quicksort(left);
|
| 8 |
+
quicksort(&mut right[1..]);
|
| 9 |
+
let mut temp = Vec::with_capacity(arr.len());
|
| 10 |
+
let (mut i, mut j) = (0, 1);
|
| 11 |
+
while i < left.len() && j < right.len() {
|
| 12 |
+
if left[i] < right[j] {
|
| 13 |
+
temp.push(left[i].clone());
|
| 14 |
+
i += 1;
|
| 15 |
+
} else {
|
| 16 |
+
temp.push(right[j].clone());
|
| 17 |
+
j += 1;
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
temp.extend_from_slice(&left[i..]);
|
| 21 |
+
temp.extend_from_slice(&right[j..]);
|
| 22 |
+
arr.clone_from_slice(&temp);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
fn main() {
|
| 26 |
+
let mut arr = vec![3, 6, 8, 10, 1, 2, 1];
|
| 27 |
+
quicksort(&mut arr);
|
| 28 |
+
println!("{:?}", arr);
|
| 29 |
+
}
|
rust/word_frequency.rs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use std::collections::HashMap;
|
| 2 |
+
use std::fs;
|
| 3 |
+
|
| 4 |
+
fn word_frequency(file_path: &str) -> HashMap<String, u32> {
|
| 5 |
+
let contents = fs::read_to_string(file_path).expect("Something went wrong reading the file");
|
| 6 |
+
let mut map = HashMap::new();
|
| 7 |
+
for word in contents.split_whitespace() {
|
| 8 |
+
let count = map.entry(word.to_string()).or_insert(0);
|
| 9 |
+
*count += 1;
|
| 10 |
+
}
|
| 11 |
+
map
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
fn main() {
|
| 15 |
+
println!("{:?}", word_frequency("sample.txt"));
|
| 16 |
+
}
|
sample.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
hello world this is a test hello world
|