Skip to content

Commit abbdd96

Browse files
authored
Merge pull request #16 from Acuspeedster/main
Added: Examples in JSON format and Updated: Docs for method-2 (addition of data to vector search)
2 parents 02aff0c + 620fe9a commit abbdd96

File tree

8 files changed

+58
-3
lines changed

8 files changed

+58
-3
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,12 @@ vector_store.add_item(
530530
)
531531
```
532532

533-
#### Method 2: Adding Multiple Examples from JSON Files
533+
### Method 2: Adding Multiple Examples from JSON Files
534+
535+
First, ensure you have the required dependencies:
536+
```bash
537+
pip install qdrant-client openai
538+
```
534539

535540
Place JSON files in the appropriate directories:
536541

@@ -551,8 +556,7 @@ Format for project examples (with optional `project_files` field):
551556
```
552557

553558
Format for error examples:
554-
555-
```
559+
```json
556560
{
557561
"error": "Rust compiler error message",
558562
"solution": "How to fix the error",
@@ -620,3 +624,5 @@ Licensed under [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html).
620624

621625

622626

627+
628+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"error": "error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable",
3+
"solution": "Move the immutable borrow into a separate scope to ensure it ends before the mutable borrow begins",
4+
"context": "This error occurs when trying to mutably borrow a value while an immutable borrow is still active. The Rust borrow checker prevents this to ensure memory safety.",
5+
"example": "// Before (error)\nfn main() {\n let mut v = vec![1, 2, 3];\n let first = &v[0];\n v.push(4); // Error: cannot borrow `v` as mutable\n println!(\"{}\", first);\n}\n\n// After (fixed)\nfn main() {\n let mut v = vec![1, 2, 3];\n {\n let first = &v[0];\n println!(\"{}\", first);\n } // immutable borrow ends here\n v.push(4); // Now it's safe to borrow mutably\n}"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"error": "error[E0106]: missing lifetime specifier",
3+
"context": "This error occurs when you return a reference from a function without specifying a lifetime parameter",
4+
"solution": "Add a lifetime parameter to the function signature and any references that need it",
5+
"example": "// Before (error)\nfn longest(x: &str, y: &str) -> &str {\n if x.len() > y.len() { x } else { y }\n}\n\n// After (fixed)\nfn longest<'a>(x: &'a str, y: &'a str) -> &'a str {\n if x.len() > y.len() { x } else { y }\n}"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"error": "error[E0277]: the trait bound `T: std::fmt::Display` is not satisfied",
3+
"solution": "Add a trait bound to the generic parameter to ensure it implements the required trait",
4+
"context": "This error occurs when using a generic type with a function that requires specific traits, but the generic parameter doesn't specify those trait bounds.",
5+
"example": "// Before (error)\nfn print_item<T>(item: T) {\n println!(\"{}\", item); // Error: `T` might not implement `Display`\n}\n\n// After (fixed)\nfn print_item<T: std::fmt::Display>(item: T) {\n println!(\"{}\", item); // Now we know T implements Display\n}\n\n// Alternative fix using where clause\nfn print_item<T>(item: T) \nwhere\n T: std::fmt::Display,\n{\n println!(\"{}\", item);\n}"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"error": "error[E0277]: the trait bound `T: std::fmt::Display` is not satisfied",
3+
"context": "This error occurs when trying to use a generic type without specifying that it needs to implement a required trait",
4+
"solution": "Add the necessary trait bound to the generic type parameter",
5+
"example": "// Before (error)\nfn print_item<T>(item: T) {\n println!(\"{}\", item);\n}\n\n// After (fixed)\nfn print_item<T: std::fmt::Display>(item: T) {\n println!(\"{}\", item);\n}"
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": "Create a command-line calculator in Rust",
3+
"example": "A simple calculator that can add, subtract, multiply and divide numbers",
4+
"project_files": {
5+
"src/main.rs": "use std::io;\n\nfn main() {\n println!(\"Simple Calculator\");\n println!(\"Enter an expression (e.g., 5 + 3):\");\n \n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"Failed to read line\");\n \n let parts: Vec<&str> = input.trim().split_whitespace().collect();\n \n if parts.len() != 3 {\n println!(\"Invalid expression! Please use format: number operator number\");\n return;\n }\n \n let a: f64 = match parts[0].parse() {\n Ok(num) => num,\n Err(_) => {\n println!(\"First value is not a valid number\");\n return;\n }\n };\n \n let operator = parts[1];\n \n let b: f64 = match parts[2].parse() {\n Ok(num) => num,\n Err(_) => {\n println!(\"Second value is not a valid number\");\n return;\n }\n };\n \n let result = match operator {\n \"+\" => a + b,\n \"-\" => a - b,\n \"*\" => a * b,\n \"/\" => {\n if b == 0.0 {\n println!(\"Error: Division by zero\");\n return;\n }\n a / b\n },\n _ => {\n println!(\"Unknown operator: {}\", operator);\n return;\n }\n };\n \n println!(\"Result: {} {} {} = {}\", a, operator, b, result);\n}",
6+
"Cargo.toml": "[package]\nname = \"calculator\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n",
7+
"README.md": "# Command-Line Calculator\n\nA simple Rust command-line calculator that supports basic arithmetic operations.\n\n## Usage\n\nRun the program and enter expressions in the format `number operator number`, such as:\n\n```\n5 + 3\n10 - 2\n4 * 7\n9 / 3\n```\n\nSupported operators: +, -, *, /"
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"query": "How to build a markdown parser in Rust",
3+
"example": "A Rust implementation of a basic markdown parser that handles headers, bold, italic, and links",
4+
"project_files": {
5+
"Cargo.toml": "[package]\nname = \"rust-markdown-parser\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\nregex = \"1.5\"",
6+
"src/main.rs": "use std::fs;\nuse regex::Regex;\n\nstruct MarkdownParser {\n input: String,\n}\n\nimpl MarkdownParser {\n fn new(input: String) -> Self {\n MarkdownParser { input }\n }\n\n fn parse_headers(&self) -> String {\n let header_regex = Regex::new(r\"^(#{1,6})\\s+(.+)$\").unwrap();\n let mut output = self.input.clone();\n\n for line in self.input.lines() {\n if let Some(captures) = header_regex.captures(line) {\n let level = captures.get(1).unwrap().as_str().len();\n let text = captures.get(2).unwrap().as_str();\n let html_header = format!(\"<h{}>{}</h{}>\", level, text, level);\n output = output.replace(line, &html_header);\n }\n }\n output\n }\n\n fn parse_bold_italic(&self, html: String) -> String {\n let bold_regex = Regex::new(r\"\\*\\*(.*?)\\*\\*\").unwrap();\n let italic_regex = Regex::new(r\"\\*(.*?)\\*\").unwrap();\n\n let bold_html = bold_regex.replace_all(&html, \"<strong>$1</strong>\");\n let italic_html = italic_regex.replace_all(&bold_html, \"<em>$1</em>\");\n \n italic_html.into()\n }\n\n fn parse_links(&self, html: String) -> String {\n let link_regex = Regex::new(r\"\\[([^\\]]+)\\]\\(([^\\)]+)\\)\").unwrap();\n link_regex.replace_all(&html, \"<a href=\\\"$2\\\">$1</a>\").into()\n }\n\n fn parse(&self) -> String {\n let headers_html = self.parse_headers();\n let formatted_html = self.parse_bold_italic(headers_html);\n let final_html = self.parse_links(formatted_html);\n final_html\n }\n}\n\nfn main() {\n let markdown = fs::read_to_string(\"input.md\").expect(\"Could not read file\");\n let parser = MarkdownParser::new(markdown);\n let html = parser.parse();\n fs::write(\"output.html\", html).expect(\"Could not write file\");\n}"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"query": "How to build a basic HTTP server in Rust",
3+
"example": "A simple Rust HTTP server that can serve static files and handle basic routes",
4+
"project_files": {
5+
"Cargo.toml": "[package]\nname = \"rust-http-server\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\ntokio = { version = \"1\", features = [\"full\"] }\nhyper = { version = \"0.14\", features = [\"full\"] }\nhttp-body = \"0.4\"\nmime = \"0.3\"",
6+
"src/main.rs": "use hyper::{Body, Request, Response, Server, Method, StatusCode};\nuse hyper::service::{make_service_fn, service_fn};\nuse std::convert::Infallible;\nuse std::net::SocketAddr;\nuse std::fs;\n\nasync fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {\n match (req.method(), req.uri().path()) {\n (&Method::GET, \"/\") => {\n let content = fs::read_to_string(\"index.html\")\n .unwrap_or_else(|_| \"Welcome to Rust HTTP Server\".to_string());\n Ok(Response::new(Body::from(content)))\n },\n (&Method::GET, \"/hello\") => {\n Ok(Response::new(Body::from(\"Hello from Rust HTTP Server!\")))\n },\n (&Method::GET, \"/files\") => {\n let files: Vec<String> = fs::read_dir(\".\")\n .unwrap()\n .filter_map(|entry| {\n entry.ok().and_then(|e| \n e.file_name().into_string().ok()\n )\n })\n .collect();\n \n let response = serde_json::to_string(&files).unwrap();\n \n Ok(Response::new(Body::from(response)))\n },\n _ => {\n let mut response = Response::new(Body::from(\"Not Found\"));\n *response.status_mut() = StatusCode::NOT_FOUND;\n Ok(response)\n }\n }\n}\n\n#[tokio::main]\nasync fn main() {\n let addr = SocketAddr::from(([127, 0, 0, 1], 8080));\n let make_svc = make_service_fn(|_conn| async {\n Ok::<_, Infallible>(service_fn(handle_request))\n });\n\n let server = Server::bind(&addr).serve(make_svc);\n\n println!(\"Server running on http://{}\", addr);\n\n if let Err(e) = server.await {\n eprintln!(\"server error: {}\", e);\n }\n}"
7+
}
8+
}

0 commit comments

Comments
 (0)