Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rust code sample #103

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,15 @@ userName
www
yourQuery
yourSourceName
htmlcontent
index.md
html
body
p
FalkorClientBuilder
FalkorConnectionInfo
h1
tokio
async
falkor
fn
61 changes: 60 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,66 @@ public class FalkorDBExample {
}
{% endcapture %}

{% include code_tabs.html id="code_tabs_0" python=python_code javascript=javascript_code java=java_code %}
{% capture rust_code %}
use falkordb::{FalkorClientBuilder, FalkorConnectionInfo};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to FalkorDB
let connection_info: FalkorConnectionInfo = "falkor://127.0.0.1:6379"
.try_into()
.expect("Invalid connection info");

let client = FalkorClientBuilder::new_async()
.with_connection_info(connection_info)
.build()
.await?;

// Select the 'MotoGP' graph
let mut graph = client.select_graph("MotoGP");

// Clear out this graph in case you've run this script before.
graph.delete().await?;

graph
.query(
r#"CREATE
(:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}),
(:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}),
(:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})"#,
)
.execute()
.await?;

// Query which riders represent Yamaha?
let mut nodes = graph
.query(
r#"MATCH (r:Rider)-[:rides]->(t:Team)
WHERE t.name = 'Yamaha'
RETURN r.name"#,
)
.execute()
.await?;

for node in nodes.data.by_ref() {
println!("{:?}", node);
}
Comment on lines +182 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve result handling in query examples

The current implementation uses debug printing and potentially unnecessary by_ref(). Consider:

-    for node in nodes.data.by_ref() {
-        println!("{:?}", node);
-    }
+    // Print rider names
+    for node in nodes.data {
+        if let Some(name) = node.get("r.name") {
+            println!("Rider: {}", name);
+        }
+    }

This change:

  1. Removes unnecessary by_ref()
  2. Adds proper error handling
  3. Improves output formatting
  4. Makes the example more educational

Also applies to: 192-194


// Query how many riders represent team Ducati?
let mut nodes = graph
.query(r#"MATCH (r:Rider)-[:rides]->(t:Team {name:'Ducati'}) RETURN count(r)"#)
.execute()
.await?;

for node in nodes.data.by_ref() {
println!("{:?}", node);
}

Ok(())
}
{% endcapture %}

{% include code_tabs.html id="code_tabs_0" python=python_code javascript=javascript_code java=java_code rust=rust_code %}

For additional demos please see visit [Demos](https://github.com/FalkorDB/demos).

Expand Down
Loading